在Swift中生成随机字母数字字符串

前端之家收集整理的这篇文章主要介绍了在Swift中生成随机字母数字字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在Swift中生成一个随机字母数字字符串?
@H_404_5@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更新

@H_404_5@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 }
原文链接:https://www.f2er.com/swift/321392.html

猜你在找的Swift相关文章