这是我的数组:
class Storages: ObservableObject {
let storage = Storage.storage()
let uid = UserAuth().uid ?? "<uid>"
@Published var photos: [String:String?] = [
"img1": UserDefaults.standard.string(forKey: "img1"),
"img2": UserDefaults.standard.string(forKey: "img2"),
"img3": UserDefaults.standard.string(forKey: "img3"),
]
这是我的看法:struct EditProfile: View {
@ObservedObject var storage = Storages()
var body: some View {
VStack {
Text("Points")
.font(Font.system(size: 21))
.fontWeight(.bold)
HStack {
ForEach(storage.photos.sorted(by: <), id: \.key){ (key,val) in
EditableCircleImage(key: key, imgName: val)
}
}
如果我将photos
字典从[String:String?]
更改为[String:String]
(通过放入?? ""
以防止nil
值-它将起作用。但是我想保留nil
值,因为我需要知道何时数组项值为零。这是我为每个传递给我的
struct
:struct EditableCircleImage: View {
@ObservedObject var storage = Storages()
let key: String
let imgName: String?
知道为什么我不能使用[String:String?]
吗?
最佳答案
试试这个变体。使用Xcode 12b上的某些复制代码进行了测试。
ForEach(storage.photos.sorted(by: { $0.key < $1.key }), id: \.key){ (key,val) in
EditableCircleImage(key: key, imgName: val)
}
关于ios - 字符串:字符串?数组而不是String:String数组在SwiftUI View 中中断了ForEach?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62535016/