Chapter 13 - onDrop modifier

Thanks for this wonderful book. Great to see something on macOS and SwiftUI as opposed to the extensive coverage of the mobile side of things.

I was implementing drag and drop in the CustomImageView in Chapter 13 and had a poke around in the documentation which said that onDrop(of:isTargeted:perform:) is deprecated. I tried the following, which appears to work:

.onDrop(
    of: [UTType.fileURL],
    isTargeted: $dragOver
) { providers in
    if let provider = providers.first {
        provider.loadDataRepresentation(
            forTypeIdentifier: UTType.fileURL.description) { data, _ in
                loadURL(from: data)
            }
    }
    return true
}
2 Likes

Hi,

Glad you’re enjoying the book.

UTType has changed to allow enum values as alternatives to strings. I updated them in some places but not here. Your solution is better than mine because it allows the types to be checked by the compiler.

I haven’t investigated it yet, but I understand that this will all change again in the new OSs due out later this year, and become cleaner and more Swifty.

Sarah

1 Like