zoukankan      html  css  js  c++  java
  • swift中反向循环

    First of all, protocol extensions change how reverse is used:

    for i in (1...5).reverse() { print(i) } // 5 4 3 2 1

    Stride has been reworked in Xcode 7 Beta 6. The new usage is:

    for i in 0.stride(to: -8, by: -2) { print(i) } // 0 -2 -4 -6
    for i in 0.stride(through: -8, by: -2) { print(i) } // 0 -2 -4 -6 -8

    It also works for Doubles:

    for i in 0.5.stride(to:-0.1, by: -0.1) { print(i) }

    Be wary of floating point compares here for the bounds.

    Earlier edit for Swift 1.2: As of Xcode 6 Beta 4, by and ReverseRange don't exist anymore :[

    If you are just looking to reverse a range, the reverse function is all you need:

    for i in reverse(1...5) { println(i) } // prints 5,4,3,2,1

    As posted by 0x7fffffff there is a new stride construct which can be used to iterate and increment by arbitrary integers. Apple also stated that floating point support is coming.

    Sourced from his answer:

    for x in stride(from: 0, through: -8, by: -2) {
        println(x) // 0, -2, -4, -6, -8
    }
    
    for x in stride(from: 6, to: -2, by: -4) {
        println(x) // 6, 2
    }
  • 相关阅读:
    我们的CPU遭到攻击[LOJ558]
    历史[ZJOI2018]
    字符串[LOJ6517]
    奥运公交[LOJ3255]
    BLO-Blockade[POI2008]
    压力[BJOI2013]
    Earthquake[USACO01OPEN]
    暴力写挂[CTSC2018]
    极简教程:数据结构与算法(二)
    DllRegisterServer的调用失败的问题解决方法
  • 原文地址:https://www.cnblogs.com/luoxiaofu/p/5815082.html
Copyright © 2011-2022 走看看