Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
你能解释一下 Swift 中 stride 的优势和它的特殊用途吗?
例如:
for i in stride(from: 0, to: 10, by: 1) { print(i) // prints from 0 to 9 }
取而代之的是,我们也可以使用for循环。
for
您的问题似乎是在询问具有范围stride的基本for循环的好处,例如1..<10.
stride
1..<10
stride支持按 1 以外的值递增。
// count by 3 for x in stride(from: 3, to: 30, by: 3) { } // Backwards for n in stride(from: 20, to: 0, by: -4) { }
stride适用于非整数值。
for r in stride(from: 3.14, to: 234.14234, by: 5.6345) { }