Fix warning "C-style for Statement is deprecated" in Swift 3

SNos

I have update Xcode to 7.3 and now I have a warning to the function that I use to create random strings.

I have tried to change the for statement with for (i in 0 ..< len){...} however, the warning became an error.

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
}
Code Different

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.

You can rewrite your loop to Swift's style:

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
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

#warning: C-style for statement is deprecated and will be removed in a future version of Swift

From Dev

c-style for statement deprecated with a twist

From Dev

C-style for statement is deprecated and will be removed in a future

From Dev

Fix for getting "deprecated and will be removed in Swift 4" warning for tableView functions

From Dev

how to fix memory leaks with xcode (Swift 3)

From Dev

Fix a MapView above a scrollable TableViewCell in Swift 3

From Dev

How to fix warning in Rails

From Java

Xcode 8 / Swift 3: "Expression of type UIViewController? is unused" warning

From Dev

Swift 3 : Warning "Unused result of call" when overriding BecomeFirstResponder

From Dev

How to fix warning 'no explicit ownership'

From Dev

How to fix leiningen :aot warning

From Dev

What is the correct way to fix this warning?

From Dev

How to fix warning init() is deprecated

From Dev

How to fix this unchecked assignment warning?

From Dev

How to fix this classification report warning?

From Dev

How to fix "WARNING: not a detached signature"?

From Dev

Alamofire and memory warning in swift

From Java

How to silence a warning in swift

From Dev

Suppress Swift compiler warning

From Dev

if let warning in Swift 2.0

From Dev

Swift warning message not clear

From Java

Warning frame for "Navigation bar" will be different at run time appears in Xcode 8 Swift 3

From Dev

Swift 3 Fetch Request Error (warning: could not load any Objective-C class information)

From Dev

How to remove the nearly matches optional requirement warning from didRegisterForRemoteNotificationsWithDeviceToken function ios swift 3?

From Dev

How to fix Warning Illegal string offset in PHP

From Dev

VLFeat - How to fix "Warning: Name is nonexistent or not a directory"?

From Dev

How to elegantly fix this unused variable warning?

From Dev

How to fix the pattern-matching exhaustive warning?

From Java

How to fix a locale setting warning from Perl?

Related Related

  1. 1

    #warning: C-style for statement is deprecated and will be removed in a future version of Swift

  2. 2

    c-style for statement deprecated with a twist

  3. 3

    C-style for statement is deprecated and will be removed in a future

  4. 4

    Fix for getting "deprecated and will be removed in Swift 4" warning for tableView functions

  5. 5

    how to fix memory leaks with xcode (Swift 3)

  6. 6

    Fix a MapView above a scrollable TableViewCell in Swift 3

  7. 7

    How to fix warning in Rails

  8. 8

    Xcode 8 / Swift 3: "Expression of type UIViewController? is unused" warning

  9. 9

    Swift 3 : Warning "Unused result of call" when overriding BecomeFirstResponder

  10. 10

    How to fix warning 'no explicit ownership'

  11. 11

    How to fix leiningen :aot warning

  12. 12

    What is the correct way to fix this warning?

  13. 13

    How to fix warning init() is deprecated

  14. 14

    How to fix this unchecked assignment warning?

  15. 15

    How to fix this classification report warning?

  16. 16

    How to fix "WARNING: not a detached signature"?

  17. 17

    Alamofire and memory warning in swift

  18. 18

    How to silence a warning in swift

  19. 19

    Suppress Swift compiler warning

  20. 20

    if let warning in Swift 2.0

  21. 21

    Swift warning message not clear

  22. 22

    Warning frame for "Navigation bar" will be different at run time appears in Xcode 8 Swift 3

  23. 23

    Swift 3 Fetch Request Error (warning: could not load any Objective-C class information)

  24. 24

    How to remove the nearly matches optional requirement warning from didRegisterForRemoteNotificationsWithDeviceToken function ios swift 3?

  25. 25

    How to fix Warning Illegal string offset in PHP

  26. 26

    VLFeat - How to fix "Warning: Name is nonexistent or not a directory"?

  27. 27

    How to elegantly fix this unused variable warning?

  28. 28

    How to fix the pattern-matching exhaustive warning?

  29. 29

    How to fix a locale setting warning from Perl?

HotTag

Archive