ios #warning:C 风格的 for 语句已被弃用,并将在 Swift 的未来版本中删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36173379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
#warning: C-style for statement is deprecated and will be removed in a future version of Swift
提问by sony
I just download a new Xcode (7.3) with swift 2.2.
我只是用 swift 2.2 下载了一个新的 Xcode (7.3)。
It has a warning:
它有一个警告:
C-style for statement is deprecated and will be removed in a future version of Swift.
C 风格的 for 语句已被弃用,并将在 Swift 的未来版本中删除。
How can I fix this warning?
我该如何解决这个警告?
回答by EI Captain v2.0
Removing for init; comparison; increment {}
and also remove ++
and --
easily. and use Swift's pretty for-in loop
删除for init; comparison; increment {}
和删除++
也--
很容易。并使用 Swift 漂亮的 for-in 循环
// WARNING: C-style for statement is deprecated and will be removed in a future version of Swift
for var i = 1; i <= 10; i += 1 {
print("I'm number \(i)")
}
Swift 2.2:
斯威夫特 2.2:
// new swift style works well
for i in 1...10 {
print("I'm number \(i)")
}
For decrement index
对于递减索引
for index in 10.stride(to: 0, by: -1) {
print(index)
}
Or you can use reverse()
like
或者你可以使用reverse()
像
for index in (0 ..< 10).reverse() { ... }
for float type (there is no need to define any types to index)
浮点型 (there is no need to define any types to index)
for index in 0.stride(to: 0.6, by: 0.1) {
print(index) //0.0 ,0.1, 0.2,0.3,0.4,0.5
}
Swift 3.0:
斯威夫特 3.0:
From Swift3.0
, The stride(to:by:)
method on Strideable has been replaced with a free function, stride(from:to:by:)
来自Swift3.0
,stride(to:by:)
Strideable 上的方法已经被替换为一个自由函数,stride(from:to:by:)
for i in stride(from: 0, to: 10, by: 1){
print(i)
}
For decrement index in Swift 3.0
, you can use reversed()
对于 in 的递减索引Swift 3.0
,您可以使用reversed()
for i in (0 ..< 5).reversed() {
print(i) // 4,3,2,1,0
}
Other then for each
and stride()
, you can use While Loops
除了 thenfor each
和stride()
,你可以使用While Loops
var i = 0
while i < 10 {
i += 1
print(i)
}
Repeat-While Loop:
Repeat-While Loop:
var a = 0
repeat {
a += 1
print(a)
} while a < 10
check out Control flows in The Swift Programming Language Guide
回答by coldfire
For this kind "for" loop:
对于这种“for”循环:
for var i = 10; i >= 0; --i {
print(i)
}
You can write:
你可以写:
for i in (0...10).reverse() {
print(i)
}
回答by SDW
I got the same error with this code:
我在这段代码中遇到了同样的错误:
for (var i = 1; i != video.getAll().count; i++) {
print("show number \(i)")
}
When you try to fix it with Xcode you get no luck... So you need to use the new swift style (for in loop):
当您尝试使用 Xcode 修复它时,您不会走运……因此您需要使用新的 swift 样式(for in loop):
for i in 1...video.getAll().count {
print("show number \(i)")
}
回答by Moin Shirazi
Blockquote
块引用
Use this instead
改用这个
if(myarr.count)
{
for i in 1...myarr?.count {
print(" number is \(i)")
}
}