'SwiftUI: Cannot make performDrop work with UTType

I am trying to implement a basic drop functionality that takes a text file, and dumps its content to a text variable. I am trying to make following code work:

import SwiftUI
import UniformTypeIdentifiers

struct FileDropDelegate: DropDelegate {
    @StateObject var textInfo: TextInfo
    
    let contentTypes: [UTType] = [.text, .plainText, .utf8PlainText, .utf16PlainText, .utf8TabSeparatedText, .utf16ExternalPlainText, .commaSeparatedText, .rtf, .rtfd, .xml, .xmlPropertyList]
    
    func performDrop(info: DropInfo) -> Bool {
        if let item = info.itemProviders(for: contentTypes).first {
            item.loadItem(forTypeIdentifier: contentTypes.description, options: nil) { (text, error) in
                DispatchQueue.main.async {
                    if let data = text as? Data {
                        let string = String(decoding: data, as: UTF8.self)
                        textInfo.clear()
                        textInfo.selection.removeAll()
                        textInfo.text = string
                        textInfo.analyze()
                    } else {
                        return
                    }
                }
            }
        }        
        return true
    }
}

I am calling .onDrop like this:

.onDrop(of: [.text, .plainText, .utf8PlainText, .utf16PlainText, .utf8TabSeparatedText, .utf16ExternalPlainText, .commaSeparatedText, .rtf, .rtfd, .xml, .xmlPropertyList], delegate: FileDropDelegate(textInfo: textInfo))

It looks like .loadItem does not take a UTType argument, and I am not sure if .description is the sensible workaround to apply here.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source