美文网首页
swift 的快速便利swift enumerated()

swift 的快速便利swift enumerated()

作者: 90后的晨仔 | 来源:发表于2020-03-06 20:05 被阅读0次
    1. for in 与 oc 中的用法还是一样
    var titesArr:[String] = ["I","AM","IS","WITH","L", "O", "V", "E", "I", "O", "S"]
    for tempObj in titesArr {
       print("测试:\(tempObj)")
        print("<==========>")
    }
    

    输入如下:

    Snip20200306_1.png
    2. enumerated() 正向快速遍历,还能拿到索引和指定的值。
    var titesArr:[String] = ["I","AM","IS","WITH","L", "O", "V", "E", "I", "O", "S"]
    for (index, text) in titesArr.enumerated() {
        print("<------------------>")
       print("index:\(index) <=> text:\(text)")
    }
    
    Snip20200306_2.png
    3. reversed() 反向遍历获取对应的索引和值。
    var titesArr:[String] = ["I","AM","IS","WITH","L", "O", "V", "E", "I", "O", "S"]
    for (index, text) in titesArr.enumerated().reversed() {
        print("<------------------>")
       print("index:\(index) <=> text:\(text)")
    }
    
    Snip20200306_3.png

    相关文章

      网友评论

          本文标题:swift 的快速便利swift enumerated()

          本文链接:https://www.haomeiwen.com/subject/xrknrhtx.html