#warning: C-style for statement is deprecated and will be removed in a future version of Swift(#warning:C 风格的 for 语句已弃用,将在 Swift 的未来版本中删除)
问题描述
我只是用 swift 2.2 下载了一个新的 Xcode (7.3).
I just download a new Xcode (7.3) with swift 2.2.
它有一个警告:
C 风格的 for 语句已弃用,并将在 Swift 的未来版本中删除.
C-style for statement is deprecated and will be removed in a future version of Swift.
如何解决此警告?
推荐答案
移除 for init;比较;增加 {} 并轻松删除 ++ 和 --.并使用 Swift 漂亮的 for-in 循环
Removing for init; comparison; increment {} and also remove ++ and -- easily. and use Swift's pretty for-in loop
// 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)")
}
<小时>
斯威夫特 2.2:
// new swift style works well
for i in 1...10 {
print("I'm number (i)")
}
<小时>
对于递减指数
for index in 10.stride(to: 0, by: -1) {
print(index)
}
或者你可以像
for index in (0 ..< 10).reverse() { ... }
<小时>
对于浮点类型(不需要定义任何类型来索引)
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:
从 Swift3.0 开始,Strideable 上的 stride(to:by:) 方法已被替换为自由函数 stride(from:to:by:)
From Swift3.0, The stride(to:by:) method on Strideable has been replaced with a free function, stride(from:to:by:)
for i in stride(from: 0, to: 10, by: 1){
print(i)
}
Swift 3.0中的递减索引,可以使用reversed()
For decrement index in Swift 3.0, you can use reversed()
for i in (0 ..< 5).reversed() {
print(i) // 4,3,2,1,0
}
除了 for each 和 stride(),你可以使用 While Loops
Other then for each and stride(), you can use While Loops
var i = 0
while i < 10 {
i += 1
print(i)
}
重复循环:
var a = 0
repeat {
a += 1
print(a)
} while a < 10
查看 Swift 编程语言指南
这篇关于#warning:C 风格的 for 语句已弃用,将在 Swift 的未来版本中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:#warning:C 风格的 for 语句已弃用,将在 Swift 的未来版本中删除
基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
