0
votes

I'm new to Swift, I'm trying to create a new Cocoa Application based on the following code:

Drag and Drop in Swift - Issues with Registering for Dragged Types?

I'm doing this:

  1. create Swift Cocoa Application
  2. add a Custom View
  3. add a Swift Class subclass of NSView named DropView
  4. set the Custom View class to DropView
  5. paste the referenced code in DropView

Then I'm getting a few errors, and I'm not really understanding why and how to solve it. Can someone help me?

Screenshot

1

1 Answers

1
votes

As the first error says, you need to add the override to your init method:

override init(frame frameRect: NSRect) {
    super.init(frame: frameRect)
}

Also there are some changes in the draggingEntered and the draggingUpdated methods. You don't need the ! for your NSDraggingInfo anymore:

override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation {
    return NSDragOperation.Copy
}

override func draggingUpdated(sender: NSDraggingInfo) -> NSDragOperation {
    return NSDragOperation.Copy
}

To check the correct implementation of a method you want to override, just start typing the method-name in your file and Xcode will create you the method with header etc:

enter image description here

To fix your last error, you need to implement the required init the NSView needs:

required init?(coder: NSCoder) {
    super.init(coder: coder)
}

To help you for further errors like these: Often Xcode itself helps you to solve the problems. Especially if you see the round error-circle on the left side of your editor. Just click on it and check what Xcode recommends:

enter image description here