I have these extensions to my NSObject, Codable
class:
extension SavingsGoal: NSItemProviderWriting {
public static let typeIdentifier = "com.AaronLBratcher.SavingsGoal.Drag"
public static var writableTypeIdentifiersForItemProvider: [String] { [typeIdentifier] }
public typealias DragHandler = (Data?, Error?) -> Void
public func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping DragHandler) -> Progress? {
do {
print("^^^ encoding")
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
completionHandler(try encoder.encode(self), nil)
} catch {
completionHandler(nil, error)
}
return nil
}
}
extension SavingsGoal: NSItemProviderReading {
public static var readableTypeIdentifiersForItemProvider: [String] { [typeIdentifier] }
public static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> SavingsGoal {
print("^^^ decoding")
let decoder = JSONDecoder()
return try decoder.decode(SavingsGoal.self, from: data)
}
}
and these onDrag
, onDrop
method calls on my Views:
.onDrag { NSItemProvider(object: goal) }
.onDrop(of: [SavingsGoal.typeIdentifier], delegate: viewModel)
Visually the drag works on the simulator, showing a plus when it hits the target View, however only the delegate's performDrop
call is made. The loadData
and object
methods are not called in the extensions and the DropInfo doesn't contain my object.
I have carefully checked and rechecked the code against available examples and cannot find what I did wrong.