Drag and Drop using swift 4 on mac OS - swift

I'm using swift 4 and Xcode 9 and I have a problem with implementation of drag and drop. I have a custom 'destination' view for drops and in swift 3 i call
register(forDraggedTypes: Array(NSURLPboardType))
to accept drags that contain those types.
How can I do somethings like this in swift 4?
Now I have this code
registerForDraggedTypes([.pdf])
And no one NSDraggingDestination method calls when I drop a pdf in my view.
(my custom view sits on top)

So my colleague found solution of this problem, in swift 4 you have to use kUTTypes casted as String for drag and drop, like this:
registerForDraggedTypes([NSPasteboard.PasteboardType(rawValue: kUTTypeFileURL as String), NSPasteboard.PasteboardType(rawValue: kUTTypeItem as String)])
With this code all NSDraggingDestination methods works fine, you can drop any file from finder in your view.

Related

Xcode + Swift: Is it possible to create custom comment marks?

In Swift (I'm using Swift 3 / Xcode 8 stable): is it possible to create custom comment mark/tags that can be recognized by Xcode? (and thus added to the jump bar.)
For example, aside from the // TODO:, // FIXME: or // MARK:, can we add new ones, like // OPTIMIZE:, // EVAL:, or any other custom mark?
Fyi, this was done in XCode 10.2.1, just in case anyone rolls past this page. Haven't tested it under XCode 8.
Funny enough was just making a //MARK: comment, and found a new trick. While you can't just to //WHATEVER: to get that showing up the same as //FIXME, etc. What you can do is this.
//MARK: WHATEVER: this also works
//MARK: ASDOESTHIS: because why not.
Screenshow also attached for proof, hope that helps.

Get value from selected picker row in WatchKit 2

I'm using Xcode 7 and I'm trying to pass a value from the picker to another function when a button is pressed. Is it possible to get the value of the row that is currently selected in the picker? Or do I have to use the picker as the button somehow? I tried implementing didSelectRow but when I try setting it up, I get an error: Use of undeclared type 'UIPicker View.' I'm guess I have to retrieve the index of the source array somehow.
UIKit is not available for Apple Watch, and it is specially designed for iOS. Else you must use WatchKit.
In order to use user interface (UI) elements in your WatchKit App (watchOS1 or 2), you must use WatchKit (WK) elements instead, such as WKInterfaceButton.
So in order to have your app running successfully, replace UIPicker (which is an UIKit element) with WKInterfacePicker (which is a WatchKit element).
Note that WKInterfacePicker will only work in watchOS 2 (or later).
You are trying to use a UIKit class.
Use Watchkit classes with watch os 2:
WKInterfacePicker

add new viewController in xCode 6

I need to add a new viewController (new file h. and m.) using xCode 6 beta. After I go to File > New > File I do not get the option to add an "Object-C class" as I used to do in xCode 5. Doing that I normally got a new h. and m. file appearing in the Navigation area.
Any one can help pls?
PS - I am a beginner trying to learn basics of "storyboard" :)
thanks
If you choose objective-c File in your screenshot , you will get a .m file and a .h file written in Objective-C
If you choose Cocoa Touch File , you will have a option of Objective-C and Swift File which can be subclasses of other cocoa touch classes such as UIViewController or UITableViewCell.
IF you choose Swift File , you will get a single empty Swift file.
Since you are a beginner writing code with Objective-C , you'd better choose Objective-c File directly
Right click on the group (folder) in the navigation area in which you want to add the new viewController. Select New File, and then select Cocoa Touch Class.

Xcode5 ( 3.5" screen to 4 inch )

Recently I had upgraded to Xcode 5 and opened a project that had been created in Xcode 4.6.1. All the images and buttons had moved around so I went back and re-arranged everything. Now a new problem has occurred, when I switch from the 4" simulator to 3.5 inch images get cut off, buttons and toolbars move around as well. I been searching for an answer for days and Im new to stackoverflow so I hope I have follow all rules and my question in the correct section.
Few notes on the project:
Using Autolayout, Size - Inferred, Orientation - Inferred
Opens in: Default 5.0, Project Deployment Target 7.0, View as: iOS 7.0 and Later
It sounds like you do not have any constraints on your objects. You will need to add them in order for autolayout to work the way you want.
If you are not wanting to add constraints programmatically than you can simply add them to objects in IB.
Click the object you want to add constraints to:
Editor -> Resolve Autolayout Issues -> Add Missing Constraints.
That will give you a good place to start with and you can further adjust the constraints from there.

How do I create a NSToggleButton in Interface Builder?

I want to make an NSToggleButton because it suits my needs perfectly: show one image when toggled on, another when it's toggled off.
From Apple's Developer website:
"After the first click, the button displays its alternate image or title; a second click returns the button to its normal state.
This option is called “Toggle” in Interface Builder’s Button Inspector.
Available in OS X v10.0 and later."
- Source
When looking at the Round Rect Button I created in Interface Builder, however, I can't find a "Toggle" attribute. My google and StackOverflow searches yield only questions around attributes for the control, but I haven't found a tutorial for creating this button in particular.
How do I create a NSToggleButton in Interface Builder? Where is it in the Object library?
The source you provdided is for OSX programming. Not iOS however this can be implemented yourself. A UIButton is just a UIView so add a UIImageView as a subview then when the button is clicked set hidden to either YES or NO
NSToggleButton is an AppKit class -- MacOS X only. You won't find it if you're building an iOS app. Classes from the Foundation framework have an NS prefix and are available in iOS (NSArray, NSDictionary, etc.), but the user interface classes in iOS are in UIKit and have a UI prefix. Take a look at UISwitch for similar functionality.