Swift 2.xで指定した長さのランダムな文字列を出力する(メモ)
サンプルメソッド
func randomStringWithLength(length: Int) -> String {
let alphabet = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
let upperBound = UInt32(alphabet.characters.count)
return String((0..<length).map { _ -> Character in
return alphabet[alphabet.startIndex.advancedBy(Int(arc4random_uniform(upperBound)))]
})
}
実行例
let str = randomStringWithLength(30)
print(str)
実行結果
V4B6ODMLAWnTIhTKS4OoPtprYs91uE