ios - Swift 如何检查我是否遍历 List[String] 的最后一项

标签 ios swift

我需要检查我何时遍历最后一项。我不能只把这一行放在我的 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/

相关文章:

swift - 以 1024 字节为单位读取大型二进制数据文件

ios - 用户界面测试 : Two elements w/same accessibilityTitle

ios - 如何在 Swift 中基本实现委托(delegate)?

ios - 当状态更改时,如何防止 SceneKit 场景重新渲染不佳?

ios - 如何用 isVideoMirrored 替换 isMirrored

swift - AVAsset 方向/旋转问题

ios - 在 UIImagePickerController 中打开录制的视频

ios - Here Map iOS Swift Crashed After call NMAApplicationContext.set(appId :. . appCode :. ..)

swift - 如何覆盖父类(super class)中的私有(private)变量

swift - 如何在 Swift 中设置全屏弹出背景