NSDragOperation, bitwise OR example?

the docs are not helpful. in fact they don't seem to be indicating anything that works.


so I'm hoping for a code example from someone who has been through this already.


the context:

I am writing a NSDraggingSource class. supporting the required method :

func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor context: NSDraggingContext) -> NSDragOperation
        
        // we would like to respond with move and copy.
        let response : NSDragOperation = NSDragOperation.copy.rawValue | NSDragOperation.move.rawValue
        return response
    }
   

as I noted, I want to provide 2 supported drag types: move, and copy.


the docs say to do this:

If the source permits dragging operations, the elements in the mask are one or more of the constants described in Obtaining Information About the Dragging Session, combined using the C bitwise OR operator.


C Bitwise OR Operator is documented to be "|"


Apple does not provide any notable examples of using this in the real world. the few 3rd party examples I have found look like this:

let result2 = inputA | inputB  
print("Result after applying | operator:",result2)
/* result
Result after applying | operator: 7 */

// note: that example is worthless in this context. I wind up with a cascade of opaque errors with unhelpful suggestions.


this is not something that can be guessed. It needs to be documented. it needs to be clear, and have multiple examples. Real ones. Not integers. Barring that tho... has anyone been through this, and can provide a straightforward example?

Answered by OOPer in 410530022

Some bitwise-OR types in ObjC are bridged as OptionSet in Swift, which uses set-like notation (similar to array) instead of bitwise-OR operator.


func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor context: NSDraggingContext) -> NSDragOperation {
      
    let response : NSDragOperation = [.copy, .move]
    return response
}


And aboout the docs, you are quite right. Many of the docs written for ObjC should be updated for Swift.

You can send bug reports of docs using the link "Report Bugs" below.

Accepted Answer

Some bitwise-OR types in ObjC are bridged as OptionSet in Swift, which uses set-like notation (similar to array) instead of bitwise-OR operator.


func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor context: NSDraggingContext) -> NSDragOperation {
      
    let response : NSDragOperation = [.copy, .move]
    return response
}


And aboout the docs, you are quite right. Many of the docs written for ObjC should be updated for Swift.

You can send bug reports of docs using the link "Report Bugs" below.

Thanks OOPer,

i knew it was going to be easier than what I was doing.

youre a lifesaver.

NSDragOperation, bitwise OR example?
 
 
Q