Skip to content

Instantly share code, notes, and snippets.

@qdequele
Created June 10, 2015 06:55
Show Gist options
  • Save qdequele/4c93270860bede3b91a4 to your computer and use it in GitHub Desktop.
Save qdequele/4c93270860bede3b91a4 to your computer and use it in GitHub Desktop.
Create a random string of n chars in swift
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