SwiftUI-PhotosSelector类型的值没有成员imageSelection



这段代码应该让用户从设备的照片库中挑选一张图像,并打印它是成功的、成功的空值还是不成功的。

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变量。

最新更新