LoginSignup
3
1

More than 3 years have passed since last update.

Swiftで指定した桁数のランダムな文字列を生成

Posted at

備忘録:book:
ユニークなIDを生成したいときなどに

func generator(_ length: Int) -> String {
    let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    var randomString = ""
    for _ in 0 ..< length {
        randomString += String(letters.randomElement()!)
    }
    return randomString
}
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1