由于某些原因,我添加了代码if user.uid == uid {return}
它将完全退出函数并且不会将任何用户添加到数组中,即使我只想过滤掉当前用户添加到数组中吗?
func fireStoreFetchUsers(){
guard let uid = Auth.auth().currentUser?.uid else { return }
let db = Firestore.firestore()
db.collection("Users")
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
let dictionary = document.data()
let user = User2(uid: "", distanceFrom: "any", dictionary: dictionary)
if user.uid == uid {return}
self.users.append(user)
self.collectionView?.reloadData()
}
}
}
}
答案 0 :(得分:1)
总结您的问题似乎是想要跳过正在迭代的数组的特定元素。这正是continue
所做的,所以只需将return
更改为continue
。
在上述更改之后,循环的最后两行不会在user.uid == uid
时执行,因此该特定用户不会被添加到users
数组并且collectionView
不会重新加载,但循环将继续使用下一个元素进行迭代,而不是从函数返回。
for document in querySnapshot!.documents {
let dictionary = document.data()
let user = User2(uid: "", distanceFrom: "any", dictionary: dictionary)
if user.uid == uid {continue}
self.users.append(user)
self.collectionView?.reloadData()
}