我正在处理一个具有拖放功能的SwiftUI项目,并且我正在使用UniformTypeIDENTIFILES框架来处理数据.然而,在Drop操作期间try 从info.itemProviders访问第一个项提供程序时遇到了一个问题.
以下是代码的相关部分:
import UniformTypeIdentifiers
class ButtonDropDelegate: NSObject, DropDelegate {
// Your delegate code and properties here...
func performDrop(info: DropInfo) -> Bool {
// Check if the drop contains data with the specified UTI (UTType.item)
if info.hasItemsConforming(to: [UTType.item]) {
// Load the dragged data as Data
if let itemProvider = info.itemProviders.first {
itemProvider.loadDataRepresentation(forTypeIdentifier: UTType.item.identifier) { data, error in
if let data = data {
do {
// Unarchive the Data to get the original [Any] array
if let keyPathArray = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? [Any] {
// Process the keyPathArray here...
print("Received keyPathArray:", keyPathArray)
}
} catch {
print("Error unarchiving data:", error)
}
}
}
}
}
return true
}
// Other delegate methods...
}
问题出在If let itemProvider=info.itemProviders.first行,其中Xcode显示错误"如果没有上下文类型,则无法解析对成员‘first’的引用".
我使用的是Xcode13,目标是iOS 15.0.如何解决此错误并正确访问Drop委托中的第一个项提供程序?任何帮助或指导都将不胜感激.谢谢.
请记住提供有关您的项目设置、任何其他相关代码以及您try 解决问题的步骤的其他详细信息,以使问题更全面.