0

I can't get my drag and drop with DataRepresentation to work with Transferable. I'm trying to drag and drop instances of DataSettings which is an NSManagedObject that conforms to NSSecureCoding.

Here's my UTType:

extension UTType {
  static var encoderSettings = UTType(exportedAs: "com.simulator.EncoderSettings")
}

Here's my conformance to Transferable:

extension DataSettings: Transferable {
  var data: Data? {
    try? NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: true)
  }

  public static var transferRepresentation: some TransferRepresentation {
    /*DataRepresentation(contentType: .commaSeparatedText) { setting in
      let data = setting.data
      print("DataRepresentation: \(data)")
      return data!
    } importing: { data in
      print("data: \(data)")
      return DataSettings()
    }*/

    DataRepresentation(contentType: .encoderSettings) { setting in
      let data = setting.data
      print("DataRepresentation: \(data)")
      return data!
    } importing: { data in
      print("data: \(data)")
      return DataSettings()
    }

//    ProxyRepresentation(exporting: \.title)
  }
}

Here's a view where I'm testing my drop destination:

struct DropTest: View {
  @State var isDropTargeted = false
  var body: some View {
    Color.pink
      .frame(width: 200, height: 200)
      .dropDestination(for: EncoderSettings.self) { setting, location in
        print("\(setting)")
        return true
      } isTargeted: {
        isDropTargeted = $0
        print("Got it!!!")
      }
  }
}

Here's my Info plist: enter image description here

The ProxyRepresentation (String) works but I need the actual Data.

The dragging starts (i.e.: I can drag the view that has the .draggable with DataSettings) but I can't drop it on my DropTest view. I can drop it on a view or app that accepts the ProxyRepresentation.

What am I missing?

Phantom59
  • 947
  • 1
  • 8
  • 19
  • After some research, problem appears to be related to NSManagedObject not permitting proper archiving and unarchiving of instances. In the end, I created a compatible class that is not an NSManagedObject but conforms to Codable. This allows use of CodableRepresentation in Transferable. – Phantom59 Feb 21 '23 at 17:22

0 Answers0