For-In Loops multiple conditions

前端 未结 5 1957
礼貌的吻别
礼貌的吻别 2021-01-18 02:34

With the new update to Xcode 7.3, a lot of issues appeared related with the new version of Swift 3. One of them says \"C-style for statement is deprecated and will be remove

5条回答
  •  北恋
    北恋 (楼主)
    2021-01-18 02:43

    It would be just as you're saying if you described it out loud:

    for i in 0 ..< min(5, products.count) { ... }
    

    That said, I suspect you really mean:

    for product in products.prefix(5) { ... }
    

    which is less error-prone than anything that requires subscripting.

    It's possible you actually need an integer index (though this is rare), in which case you mean:

    for (index, product) in products.enumerate().prefix(5) { ... }
    

    Or you could even get a real index if you wanted with:

    for (index, product) in zip(products.indices, products).prefix(5) { ... }
    

提交回复
热议问题