I have update Xcode
to 7.3 and now I have a warning to the function that I use to create random strings.
我已经将Xcode更新为7.3,现在我对用于创建随机字符串的函数有一个警告。
I have tried to change the for
statement with for (i in 0 ..< len){...}
however, the warning became an error.
我已经试着把for语句改为for (I in 0 .)< len){…然而,这个警告变成了一个错误。
How can I remove the warning?
我如何删除警告?
static func randomStringWithLength (len : Int) -> NSString {
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let randomString : NSMutableString = NSMutableString(capacity: len)
for (var i=0; i < len; i += 1){ // warning
let length = UInt32 (letters.length)
let rand = arc4random_uniform(length)
randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
}
return randomString
}
4 个解决方案
#1
44
C-style for
loop has been deprecated in Swift 3. You can continue using it for a while, but they will certainly disappear in the future.
for循环的c风格已经在Swift 3中被弃用。您可以继续使用它一段时间,但是它们肯定会在将来消失。
You can rewrite your loop to Swift's style:
你可以把你的循环改写成Swift的风格:
for i in 0..<len {
let length = UInt32 (letters.length)
let rand = arc4random_uniform(length)
randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
}
Since you don't use i
at all in the loop's body, you can replace it with:
因为你不用我在循环的身体里,你可以用:
for _ in 0..<len {
// do stuffs
}
#2
11
This BLOG saved my life.
这个博客救了我一命。
INCREMENTING
递增
for i in 0 ..< len {
}
DECREMENTING
递减
for i in (0 ..< len).reverse() {
}
NON-SEQUENTIAL INDEXING
非时序的索引
Using where
使用在哪里
for i in (0 ..< len) where i % 2 == 0 {
}
Using striding to or through
用大步走或通过。
for i in 0.stride(to: len, by: 2) {
}
#3
9
in Swift 3 it's been an error
在Swift 3中,这是一个错误。
some general replacement was posted and just add
一些一般性的替换被发布并添加。
For Swift 3 and need to change the "index"
对于Swift 3,需要更改“索引”
for var index in stride(from: 0, to: 10, by: 1){}
#4
5
I've had success with the following. You can use the for loop as follows - note that the for loop is inclusive so you may need to check that len is actually greater than 0:
我已经成功了。您可以使用for循环语句—注意for循环是包含在内的,因此您可能需要检查len是否大于0:
for i in 0...len - 1 {
let length = UInt32 (letters.length)
let rand = arc4random_uniform(length)
randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
}
Or you can use this:
或者你可以用这个:
for i in 0 ..< len {
let length = UInt32 (letters.length)
let rand = arc4random_uniform(length)
randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
}
BTW it appears XCode 7.x does help you to get there but it's a two step process. First you have to change your increment operator from (say) i++ to i += 1 and then XCode warning helps you modify the loop.
顺便说一下,它是XCode 7。x可以帮助你到达那里,但这是两个步骤。首先,你必须改变你的增量运算符,从(比方说)i++到i+ = 1,然后XCode警告帮助你修改循环。
#1
44
C-style for
loop has been deprecated in Swift 3. You can continue using it for a while, but they will certainly disappear in the future.
for循环的c风格已经在Swift 3中被弃用。您可以继续使用它一段时间,但是它们肯定会在将来消失。
You can rewrite your loop to Swift's style:
你可以把你的循环改写成Swift的风格:
for i in 0..<len {
let length = UInt32 (letters.length)
let rand = arc4random_uniform(length)
randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
}
Since you don't use i
at all in the loop's body, you can replace it with:
因为你不用我在循环的身体里,你可以用:
for _ in 0..<len {
// do stuffs
}
#2
11
This BLOG saved my life.
这个博客救了我一命。
INCREMENTING
递增
for i in 0 ..< len {
}
DECREMENTING
递减
for i in (0 ..< len).reverse() {
}
NON-SEQUENTIAL INDEXING
非时序的索引
Using where
使用在哪里
for i in (0 ..< len) where i % 2 == 0 {
}
Using striding to or through
用大步走或通过。
for i in 0.stride(to: len, by: 2) {
}
#3
9
in Swift 3 it's been an error
在Swift 3中,这是一个错误。
some general replacement was posted and just add
一些一般性的替换被发布并添加。
For Swift 3 and need to change the "index"
对于Swift 3,需要更改“索引”
for var index in stride(from: 0, to: 10, by: 1){}
#4
5
I've had success with the following. You can use the for loop as follows - note that the for loop is inclusive so you may need to check that len is actually greater than 0:
我已经成功了。您可以使用for循环语句—注意for循环是包含在内的,因此您可能需要检查len是否大于0:
for i in 0...len - 1 {
let length = UInt32 (letters.length)
let rand = arc4random_uniform(length)
randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
}
Or you can use this:
或者你可以用这个:
for i in 0 ..< len {
let length = UInt32 (letters.length)
let rand = arc4random_uniform(length)
randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
}
BTW it appears XCode 7.x does help you to get there but it's a two step process. First you have to change your increment operator from (say) i++ to i += 1 and then XCode warning helps you modify the loop.
顺便说一下,它是XCode 7。x可以帮助你到达那里,但这是两个步骤。首先,你必须改变你的增量运算符,从(比方说)i++到i+ = 1,然后XCode警告帮助你修改循环。