Generate random alphanumeric string in Swift

前端 未结 22 1018
暖寄归人
暖寄归人 2020-11-27 09:51

How can I generate a random alphanumeric string in Swift?

22条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 10:21

    Swift 2.2 Version

    // 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..

    Basically call this method that will generate a random string the length of the integer handed to the function. To change the possible characters just edit the charactersString string. Supports unicode characters too.

    https://gist.github.com/gingofthesouth/54bea667b28a815b2fe33a4da986e327

提交回复
热议问题