本文介绍了在Cocoa中生成一个随机字母数字字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想调用一个方法,传递它的长度,并让它生成一个随机的字母数字字符串。
I want to call a method, pass it the length and have it generate a random alphanumeric string.
有任何实用程序库,这些类型的函数?
Are there any utility libraries out there that may have a bunch of these types of functions?
推荐答案
尚未经过测试。
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
-(NSString *) randomStringWithLength: (int) len {
NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
for (int i=0; i<len; i++) {
[randomString appendFormat: @"%C", [letters characterAtIndex: arc4random_uniform([letters length])]];
}
return randomString;
}
这篇关于在Cocoa中生成一个随机字母数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!