I'm trying to implement a simple drag and drop feature with the transferable protocol. In my application, I have a Task struct as follows:
struct Task: Identifiable, Hashable, Equatable, Codable, Transferable{
var id = UUID()
var description: String
var status: Int = 0
var tags: [Tag] = []
var inKanBan: Bool = false
var archived: Bool = false
init(description: String){
self.description = description
}
static var transferRepresentation: some TransferRepresentation {
CodableRepresentation(contentType: .task)
}
}
extension UTType {
static let task: UTType = UTType(exportedAs: "some.app.Task")
}
And my test view is as follows:
var body: some View {
HStack {
VStack{
ForEach(taskManager.getKanBanToDoList(), id:\.self){ task in
RoundedRectangle(cornerRadius: 5)
.fill(.blue)
.frame(width:50, height: 20)
.foregroundColor(.white)
.overlay{
Text(task.description)
}
.draggable(task)
}
}
Color.pink
.frame(width: 400, height: 400)
.opacity(0.5)
.dropDestination(for: Task.self) { items, location in
print("dropping \(items.first!.id)")
return true
} isTargeted: { inDrop in
print("targeted..")
}
}
}
I've also added the "Exported Type Identifiers".
The problem is, this code is working when I set Supported Destination as Mac Catalyst. However, it is not working if the destination is macOS. As far as I know, Transferable protocol is working in macOS too. When I change the code for Text views, the code is working. But not for the custom types. What would be the problem? Am i missing something?