我需要检查我何时遍历最后一项。我不能只把这一行放在我的 for 循环之后,因为那样我总是收到一个空列表。我尝试了以下但这个不起作用:
.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists(){
for rest in snapshot.children.allObjects.count as! [DataSnapshot] {
let refi = Database.database().reference().child("Users")
refi.observeSingleEvent(of: .value, with: { (snapshoti) in
if snapshoti.value as! String == "active"{
let userid = rest.key
self.someProtocol[rest.key] = self.checksum
if self.checksum == self.someProtocol.count-1 {
self.sortUsers()
}else{
}
self.checksum = self.checksum+1
}
})
}
最佳答案
dr_barto 的答案可行,但需要进行以下调整:
for (idx, element) in array.enumerated() {
if idx == array.endIndex-1 {
// handling the last element
}
}
来自 Apple 文档:
endIndex is the array’s “past the end” position—that is, the position one greater than the last valid subscript argument
关于ios - Swift 如何检查我是否遍历 List[String] 的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51209600/