Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Where Is NSDragOperationNone?

In Objective-C, I can return NSDragOperationNone from various drag/drop methods to indicate that I do not want to handle a given drag. This is listed explicitly in Apple's Docs for the Objective-C version of NSDragOperation:

https://developer.apple.com/documentation/appkit/nsdragoperation?language=objc

However, if you view that page for Swift, or you look at the NSDragOperation definition in Apple's Swift headers, .none is not an option.

So...how do I decline a drag operation in Swift? In a method such as:

func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation

NOTE:

1) I am explicitly dealing with AppKit and macOS, not UIKit or iOS.

2) I realize I can just init NSDragOperation with a rawvalue of 0, which is the value given to NSDragOperationNone in Objective-C, but that's fragile. What is the Swift way of declining a drag?

like image 909
Bryan Avatar asked Jan 02 '23 04:01

Bryan


2 Answers

You might have realised this already, but NSDragOperation is a bit mask. The value of each case is a power of 2, and you are supposed to use | to mask them in Objective-C.

In Swift, NSDragOperation is bridged to conform to OptionSet and you can use them with a set-like syntax like this:

let dragOp: NSDragOperation = [.copy, .link]

So a none operation is just an empty set:

let dragOp: NSDragOperation = []
like image 58
Sweeper Avatar answered Jan 03 '23 16:01

Sweeper


Use []. .none is deprecated. See https://forums.developer.apple.com/thread/65205

like image 36
babibo Avatar answered Jan 03 '23 18:01

babibo