仅当变量从 IOS 应用中的云端更新时更新文本字段
Update Text Field only when Variable updates from the cloud in IOS app
我正在使用 Firebase 为数组附加数据字符串。然后我想在屏幕上显示这些数据。屏幕上的视图是一个列表,每个 Post 正在查看数组的不同部分(例如;postName[0] 与 postName[1]。
我的问题是,屏幕加载后,数据尚未完全从云端加载,因此数组为空。这会导致应用程序崩溃并提示“索引超出范围”,因为数组中没有文本框试图读取的内容。
数组从 Firebase 接收数据,如果数据到达的速度足够快,则不会出现任何问题,但是,有时数据的速度不够快并崩溃,提示索引不在范围内。
有什么我可以设置的,以便在数据加载完成之前不加载文本字段?
提供的代码:
List(fetchPostModel.postsNearby, id: \.self) { post in
ZStack {
if !fetchPostModel.postName.isEmpty { Text(fetchPostModel.postName[Int(post) ?? 0])
.font(.title)
.bold()
.padding()
} else { Text("Loading...").font(.title).bold().padding() }
}
.onAppear {
fetchFromCloud(postNumber: fetchFromCloud.postNumber[Int(post) ?? 0])
}
}
为了防止“索引超出范围”,您可以先打开 属性 以避免 Int(post) ?? 0
if !fetchPostModel.postName.isEmpty {
if let postIndex = post {
Text(fetchPostModel.postName[Int(postIndex)])
.font(.title)
.bold()
.padding()
}
} else { Text("Loading...").font(.title).bold().padding() }
您可以为此创建一个扩展程序
extension Collection where Indices.Iterator.Element == Index {
public subscript(safe index: Index) -> Iterator.Element? {
return (startIndex <= index && index < endIndex) ? self[index] : nil
}
}
现在它可以在不同的集合中普遍应用
示例:
[1, 2, 3][safe: 4] // 数组 - 打印 'nil'
(0..<3)[safe: 4] // 范围 - 打印 'nil'
在你的问题中,你可以这样使用
List(fetchPostModel.postsNearby, id: \.self) { post in
ZStack {
if let currentPostName = fetchPostModel.postName[safe: post] {
Text(currentPostName)
.font(.title)
.bold()
.padding()
} else {
Text("Loading...").font(.title).bold().padding()
}
}
.onAppear {
fetchFromCloud(postNumber: fetchFromCloud.postNumber[Int(post) ?? 0])
}
}
我正在使用 Firebase 为数组附加数据字符串。然后我想在屏幕上显示这些数据。屏幕上的视图是一个列表,每个 Post 正在查看数组的不同部分(例如;postName[0] 与 postName[1]。
我的问题是,屏幕加载后,数据尚未完全从云端加载,因此数组为空。这会导致应用程序崩溃并提示“索引超出范围”,因为数组中没有文本框试图读取的内容。
数组从 Firebase 接收数据,如果数据到达的速度足够快,则不会出现任何问题,但是,有时数据的速度不够快并崩溃,提示索引不在范围内。
有什么我可以设置的,以便在数据加载完成之前不加载文本字段?
提供的代码:
List(fetchPostModel.postsNearby, id: \.self) { post in
ZStack {
if !fetchPostModel.postName.isEmpty { Text(fetchPostModel.postName[Int(post) ?? 0])
.font(.title)
.bold()
.padding()
} else { Text("Loading...").font(.title).bold().padding() }
}
.onAppear {
fetchFromCloud(postNumber: fetchFromCloud.postNumber[Int(post) ?? 0])
}
}
为了防止“索引超出范围”,您可以先打开 属性 以避免 Int(post) ?? 0
if !fetchPostModel.postName.isEmpty {
if let postIndex = post {
Text(fetchPostModel.postName[Int(postIndex)])
.font(.title)
.bold()
.padding()
}
} else { Text("Loading...").font(.title).bold().padding() }
您可以为此创建一个扩展程序
extension Collection where Indices.Iterator.Element == Index {
public subscript(safe index: Index) -> Iterator.Element? {
return (startIndex <= index && index < endIndex) ? self[index] : nil
}
}
现在它可以在不同的集合中普遍应用
示例:
[1, 2, 3][safe: 4] // 数组 - 打印 'nil'
(0..<3)[safe: 4] // 范围 - 打印 'nil'
在你的问题中,你可以这样使用
List(fetchPostModel.postsNearby, id: \.self) { post in
ZStack {
if let currentPostName = fetchPostModel.postName[safe: post] {
Text(currentPostName)
.font(.title)
.bold()
.padding()
} else {
Text("Loading...").font(.title).bold().padding()
}
}
.onAppear {
fetchFromCloud(postNumber: fetchFromCloud.postNumber[Int(post) ?? 0])
}
}