-
-
Save omgbbqhaxx/f51f7cbc069bfa6415e5a4a401e06c75 to your computer and use it in GitHub Desktop.
Swift 2.2 Random String Generation
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
// based on https://gist.github.com/samuel-mellert/20b3c99dec168255a046 | |
// which is based on https://gist.github.com/szhernovoy/276e69eb90a0de84dd90 | |
// Updated to work on Swift 2.2 | |
func randomString(length: Int) -> String { | |
let charactersString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | |
let charactersArray : [Character] = Array(charactersString.characters) | |
var string = "" | |
for _ in 0..<length { | |
string.append(charactersArray[Int(arc4random()) % charactersArray.count]) | |
} | |
return string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment