Created
June 10, 2015 06:55
-
-
Save qdequele/4c93270860bede3b91a4 to your computer and use it in GitHub Desktop.
Create a random string of n chars in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func randomStringWithLength (len : Int) -> NSString { | |
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | |
var randomString : NSMutableString = NSMutableString(capacity: len) | |
for (var i=0; i < len; i++){ | |
var length = UInt32 (letters.length) | |
var rand = arc4random_uniform(length) | |
randomString.appendFormat("%C", letters.characterAtIndex(Int(rand))) | |
} | |
return randomString | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment