#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 的未来版本中删除


- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 拆分 Drawable 2022-01-01