Generate random alphanumeric string in Swift

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

How can I generate a random alphanumeric string in Swift?

22条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 10:07

    Simple and Fast -- UUID().uuidString

    // Returns a string created from the UUID, such as "E621E1F8-C36C-495A-93FC-0C247A3E6E5F"

    public var uuidString: String { get }

    https://developer.apple.com/documentation/foundation/uuid

    Swift 3.0

    let randomString = UUID().uuidString //0548CD07-7E2B-412B-AD69-5B2364644433
    print(randomString.replacingOccurrences(of: "-", with: ""))
    //0548CD077E2B412BAD695B2364644433
    

    EDIT

    Please don't confuse with UIDevice.current.identifierForVendor?.uuidString it won't give random values.

提交回复
热议问题