func randomStringWithLength (len : Int) -> Nsstring { let letters : Nsstring = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMnopQRSTUVWXYZ0123456789" var randomString : NSMutableString = NSMutableString(capacity: len) for (var i=0; i < len; i++){ var length = UInt32 (letters.length) var rand = arc4random_uniform(length) randomString.appendFormat("%C",letters.characteratIndex(Int(rand))) } return randomString}
Swift 3.0更新
func randomString(length: Int) -> String { let letters : Nsstring = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMnopQRSTUVWXYZ0123456789" let len = UInt32(letters.length) var randomString = "" for _ in 0 ..< length { let rand = arc4random_uniform(len) var nextChar = letters.character(at: Int(rand)) randomString += Nsstring(characters: &nextChar,length: 1) as String } return randomString}总结
以上是内存溢出为你收集整理的在Swift中生成随机字母数字字符串全部内容,希望文章能够帮你解决在Swift中生成随机字母数字字符串所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)