#warning:C语言的陈述已被弃用,并将在未来版本的Swift中删除
我只是用swift 2.2下载一个新的Xcode(7.3)。
它有一个警告:
C语言的语句已被弃用,并将在未来版本的Swift中被删除。
我怎样才能解决这个警告?
删除for init; comparison; increment {}
for init; comparison; increment {}
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:
// 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) }
或者你可以使用reverse()
for index in (0 ..< 10).reverse() { ... }
对于浮点types(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:
从Swift3.0
, Swift3.0
的stride(to:by:)
方法已被replace为一个自由函数, stride(from:to:by:)
for i in stride(from: 0, to: 10, by: 1){ print(i) }
对于Swift 3.0
递减索引,可以使用reversed()
for i in (0 ..< 5).reversed() { print(i) // 4,3,2,1,0 }
除此之外, for each
和stride()
,你可以使用While Loops
var i = 0 while i < 10 { i += 1 print(i) }
Repeat-While Loop:
var a = 0 repeat { a += 1 print(a) } while a < 10
检查“Swift编程语言指南”中的控制stream
对于这种“for”循环:
for var i = 10; i >= 0; --i { print(i) }
你可以写:
for i in (0...10).reverse() { print(i) }
我得到了与此代码相同的错误:
for (var i = 1; i != video.getAll().count; i++) { print("show number \(i)") }
当你尝试用Xcode修复它时,你没有运气…所以你需要使用新的swift样式(for循环):
for i in 1...video.getAll().count { print("show number \(i)") }
大段引用
改用这个
if(myarr.count) { for i in 1...myarr?.count { print(" number is \(i)") } }