开发者

Generate string of random characters cocoa?

I know how to generate random numbers, but what I really need is a string of random characters. This is what I 开发者_C百科have so far:

NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789?%$";

    char generated;
    generated = //how do i define this?

    NSLog(@"generated =%c", generated);
    [textField setStringValue:generated];


-(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;
}

then Call it like this for a 5 letter string:

NSString* string = [self generateRandomString:5];


See this SO Q & A.

Generate a random alphanumeric string in Cocoa

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜