如何解决SwiftUI:在 macOS 12 Monterey 上更改了通过粘贴板拖动?
我正在尝试在 SwiftUI 中对 NSTextView 执行基本的拖放操作,但在 macOS 12 beta 中发生了一些变化,虽然此代码在 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)
}
}
在 macOS 12 beta 上,调用 pboard.readobjects(...) 时,尝试将项目拖入 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?}
我注意到 supportsSecureCoding
和 encode(with aCoder: NSCoder)
都不是 macOS 12,而是在 macOS 11 上。
macOS 12 中是否有关于 NSCoding 或 NSSecureCoding 的任何更改,或者这只是一个错误?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。