我知道如何生成随机数,但我真正需要的是一串随机字符。这就是我到目前为止所做的:
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789?%$";
char generated;
generated = //how do i define this?
NSLog(@"generated =%c", generated);
[textField setStringValue:generated];
答案 0 :(得分:2)
-(NSString*)generateRandomString:(int)num {
NSMutableString* string = [NSMutableString stringWithCapacity:num];
for (int i = 0; i < num; i++) {
[string appendFormat:@"%C", (unichar)('a' + arc4random_uniform(25))];
}
return string;
}
然后将其称为5个字母的字符串:
NSString* string = [self generateRandomString:5];
答案 1 :(得分:1)
见SO Q&amp;甲