这段代码应该让用户从设备的照片库中挑选一张图像,并打印它是成功的、成功的空值还是不成功的。
struct PhotosSelector: View {
@State var selectedItems: [PhotosPickerItem] = []
var body: some View {
PhotosPicker(selection: $selectedItems,
matching: .images) {
Text("Select a Photo")
}
}
func loadTransferable(from imageSelection: PhotosPickerItem) -> Progress {
return imageSelection.loadTransferable(type: Image.self) { result in
DispatchQueue.main.async {
guard imageSelection == self.imageSelection else { return }
switch result {
case .success(let image?):
print("Success.")
case .success(nil):
print("Success with empty value.")
case .failure(let error):
print("Failure.")
}
}
}
}
}
不幸的是,guard imageSelection == self.imageSelection else { return }
不断抛出以下错误:
"PhotosSelector"类型的值没有成员"imageSelection">
我不确定我做错了什么,希望你能给我指明正确的方向。
你尝试过没有自我
例如self.imageSelection
->imageSelection
或guard imageSelection else { return }
您正在尝试从PhotosSelector
使用imageSelection
,而不是从func loadTransferable
使用
因为PhotoSelector
没有imageSelection
变量。