Problems with draggable and dropDestination using DataRepresentation in Transferable

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:

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?

Did you ever find a solution for this? I'm trying to do a similar thing, drag and drop instances of an NSManagedObject. Doing all the things you did yields the same result for me - I can drag but can't drop.

Same here. At first I thought it was a problem with cycles in my Data Models, but I created a very simple DataModel with just an id: UUID and name: string. Dragging works perfectly, but dropping is not possible:

            VStack{
                ForEach(dragModels, id: \.id) { dragModel in
                    Text("dragModel")
                        .draggable(dragModel)
                }
                RoundedRectangle(cornerRadius: 3.0)
                    .frame(width:200, height: 100)
                    .dropDestination(for: DragModel.self) { droppedDrags, _ in
                        for drag in droppedDrags {
                            print(drag.name) // Accessing the name property directly
                        }
                        return true
                    } isTargeted: { isTargeted in
                        isDropDestinationTargeted = isTargeted
                    }
                    .foregroundColor(isDropDestinationTargeted ? Color.green : Color.gray)
                    .opacity(0.5)
                    .frame(height: 100)
            }

with this:

import Foundation

import Foundation
import SwiftData
import UniformTypeIdentifiers
import SwiftUI

@Model
final class DragModel: Codable, Transferable {
    @Attribute(.unique) var id: UUID
    var name: String
    
    init(name: String) {
        self.id = UUID()
        self.name = name
    }
    
    // Codable conformance
    enum CodingKeys: CodingKey {
        case id, name
    }
    
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        id = try container.decode(UUID.self, forKey: .id)
        name = try container.decode(String.self, forKey: .name)
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(name, forKey: .name)
    }
    
    // Transferable conformance
    static var transferRepresentation: some TransferRepresentation {
        CodableRepresentation(contentType: .dragModel)
    }
}

extension UTType {
    static let dragModel = UTType(exportedAs: "com.yannic.dragModel")
}

And added to the Exported Type Identifiers

Problems with draggable and dropDestination using DataRepresentation in Transferable
 
 
Q