This code works fine on macOS 11:
var body: some View {
VStack {
ActionControl()
.padding()
.onDrag { () -> NSItemProvider in
let value = Action(string: "hello, world").string
let p = ActionProfile(value: value)
return NSItemProvider(item: p, typeIdentifier: ActionProfile.pasteboardType)
}
MyTextView()
.padding()
}
}
class ActionProfile: NSObject, NSCoding, NSSecureCoding {
static var supportsSecureCoding: Bool = true
static var pasteboardType = "com.my.app.action.profile"
@objc var rawValue: String
func encode(with aCoder: NSCoder) {
aCoder.encode(rawValue, forKey: "value")
}
required init(value: String) {
self.rawValue = value
}
required init?(coder aDecoder: NSCoder) {
self.rawValue = aDecoder.decodeObject(of: NSString.self, forKey: "value")! as String
}
required init?(pasteboardPropertyList propertyList: Any, ofType type: NSPasteboard.PasteboardType) {
return nil
}
}
extension ActionProfile: NSPasteboardWriting, NSPasteboardReading {
static var nsPasteboardType: NSPasteboard.PasteboardType = .init(pasteboardType)
static func readingOptions(forType type: NSPasteboard.PasteboardType, pasteboard: NSPasteboard) -> NSPasteboard.ReadingOptions {
return .asKeyedArchive
}
func writableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] {
return [ActionProfile.nsPasteboardType]
}
func pasteboardPropertyList(forType type: NSPasteboard.PasteboardType) -> Any? {
if type == ActionProfile.nsPasteboardType {
return try! NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: false)
}
return nil
}
static func readableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] {
return [ActionProfile.nsPasteboardType]
}
}
extension MyTextViewControl {
override internal var writablePasteboardTypes: [NSPasteboard.PasteboardType] {
return [ActionProfile.nsPasteboardType] + super.writablePasteboardTypes
}
override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
return true
}
override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
let location = self.characterIndexForInsertion(at: self.convert(sender.draggingLocation, from: nil))
self.setSelectedRange(NSRange(location: location, length: 0))
return sender.draggingSource is MyTextViewControl ? .move : .copy
}
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
let pboard = sender.draggingPasteboard
if pboard.availableType(from: [ActionProfile.nsPasteboardType]) == ActionProfile.nsPasteboardType {
if let profiles = pboard.readObjects(forClasses: [ActionProfile.self], options: nil) as? [ActionProfile], !profiles.isEmpty {
let alert = NSAlert()
alert.messageText = "WORKS"
alert.runModal()
return true
}
else {
let alert = NSAlert()
alert.messageText = "FAILED"
alert.runModal()
return super.performDragOperation(sender)
}
}
return super.performDragOperation(sender)
}
}
On macOS 12 beta, when calling pboard.readObjects(...)
I get this error when trying to drag the item into a NSTextView:
Failed to initialize keyed unarchiver for pasteboard data: Error Domain=NSCocoaErrorDomain Code=4864 "*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: data is empty; did you forget to send -finishEncoding to the NSKeyedArchiver?" UserInfo={NSDebugDescription=*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: data is empty; did you forget to send -finishEncoding to the NSKeyedArchiver?}
I've noticed that encode(with aCoder: NSCoder)
isn't called at all on macOS 12 but is on macOS 11.
Are there any changes in macOS 12 regarding NSCoding or NSSecureCoding or is this just a bug?