在IOS上创建随机字母数字

在IOS上创建随机字母数字,ios,objective-c,Ios,Objective C,我是一名java程序员,“必须”使用obj-C一段时间 我在生成随机字母数字代码时有些困惑。。。下面是我的Java代码: PS:我想生成这样的代码:Gh12PU67、AC88pP13、Bk81gH89 private String generateCode(){ String code = ""; Random r = new Random(); char[] c = new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m

我是一名java程序员,“必须”使用obj-C一段时间

我在生成随机字母数字代码时有些困惑。。。下面是我的Java代码:

PS:我想生成这样的代码:Gh12PU67、AC88pP13、Bk81gH89

private String generateCode(){
 String code = "";
 Random r = new Random();
 char[] c = new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

 for(int i = 0; i<4; i++){
  int uplow = r.nextInt(2);
  String temp = ""+ c[r.nextInt(c.length)];
  if(uplow==1)
   code = code + temp.toUpperCase();
 else
   code = code + temp;

 if((i+1)%2==0){
   code += r.nextInt(10);
   code += r.nextInt(10);
 }
}

return code;
}
private String generateCode(){
字符串代码=”;
随机r=新随机();
char[]c=新字符[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

对于(inti=0;i您的Objective-C代码看起来不错,但是(正如@Wain在上面的评论中正确地说的那样), Java函数包含在两个字母后插入两位数字的逻辑,您可以 未在Objective-C方法中复制

我会让这个逻辑稍微不那么晦涩,把它写成

- (void)generateCode
{
    static NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY";
    static NSString *digits = @"0123456789";
    NSMutableString *s = [NSMutableString stringWithCapacity:8];
    for (NSUInteger i = 0; i < 2; i++) {
        uint32_t r;

        // Append 2 random letters:
        r = arc4random_uniform((uint32_t)[letters length]);
        [s appendFormat:@"%C", [letters characterAtIndex:r]];
        r = arc4random_uniform((uint32_t)[letters length]);
        [s appendFormat:@"%C", [letters characterAtIndex:r]];

        // Append 2 random digits:
        r = arc4random_uniform((uint32_t)[digits length]);
        [s appendFormat:@"%C", [digits characterAtIndex:r]];
        r = arc4random_uniform((uint32_t)[digits length]);
        [s appendFormat:@"%C", [digits characterAtIndex:r]];

    }
    NSLog(@"s-->%@",s);
}
它从随机数创建一个
NSNumber
对象
@(r)
,然后
将其转换为字符串。

如果需要安全的随机字符串,应使用以下代码:

#define ASCII_START_NUMERS 0x30
#define ASCII_END_NUMERS 0x39

#define ASCII_START_LETTERS_A 0x41
#define ASCII_END_LETTERS_Z 0x5A

#define ASCII_START_LETTERS_a 0x61
#define ASCII_END_LETTERS_z 0x5A

-(NSString *)getRandomString:(int)length {
    NSMutableString *result = [[NSMutableString alloc]init];
    while (result.length != length) {
        NSMutableData* data = [NSMutableData dataWithLength:1];
        SecRandomCopyBytes(kSecRandomDefault, 1, [data mutableBytes]);
        Byte currentChar = 0;
        [data getBytes:&currentChar length:1];
        NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        if (currentChar > ASCII_START_NUMERS && currentChar < ASCII_END_NUMERS) { // 0 to 0
            [result appendString:s];
            continue;
        }
        if (currentChar > ASCII_START_LETTERS_A && currentChar < ASCII_END_LETTERS_Z) { // A to Z
            [result appendString:s];
            continue;
        }
        if (currentChar > ASCII_START_LETTERS_a && currentChar < ASCII_END_LETTERS_z) { // a to z
            [result appendString:s];
            continue;
        }
    }
    return result;
}
#定义ASCII_开始_数字0x30
#定义ASCII\u结束\u数字0x39
#定义ASCII\u开始字母\u 0x41
#定义ASCII_END_字母_Z 0x5A
#定义ASCII\u开始字母\u 0x61
#定义ASCII_END_字母_z 0x5A
-(NSString*)getRandomString:(int)长度{
NSMutableString*结果=[[NSMutableString alloc]init];
while(result.length!=长度){
NSMutableData*数据=[NSMutableData数据长度:1];
SecRandomCopyBytes(kSecRandomDefault,1,[数据可变字节]);
字节currentChar=0;
[data getBytes:¤tChar长度:1];
NSString*s=[[NSString alloc]initWithData:数据编码:NSUTF8StringEncoding];
如果(currentChar>ASCII\u开始\u数字&¤tCharASCII_开始字母_A&¤tCharASCII_开始字母_a&¤tChar
首先,如果需要8个字符,则需要从0到8。然后,为字符串的每个部分使用单独的字母表。您好@H2CO3,您能帮我修复该代码吗?我对这种语言完全是新手和新手:(您的代码具有正确的结构,但您没有尝试复制逻辑。这不是语言问题,您只是没有完成完整的映射。您可能希望生成UUID:
NSLog(@“%@,[[nsuid UUID]uUIString])
这与您的模式不匹配,但这是
更随机的
alphanumsequence@PetroKorienev注:)那么在obj-C中,字符串随机和数字随机之间有两种方法吗?@YAZID:我不理解这个问题。这是相同的方法。字母是从
字母
字符串中随机选择的,数字是从
数字
字符串中随机选择的。@YAZID:据我所知,Java代码
code+=r.nextInt(10)
计算一个随机整数0..9,并隐式地将其转换为一个字符并附加到字符串中。您也可以在Objective-C中执行类似的操作,我已相应地更新了答案。yeap..您运行的代码,,,哈哈,yeah man,,不像java那样简单:)…但感谢您的回答:)
r = arc4random_uniform(10);
[s appendString:[@(r) stringValue]];
#define ASCII_START_NUMERS 0x30
#define ASCII_END_NUMERS 0x39

#define ASCII_START_LETTERS_A 0x41
#define ASCII_END_LETTERS_Z 0x5A

#define ASCII_START_LETTERS_a 0x61
#define ASCII_END_LETTERS_z 0x5A

-(NSString *)getRandomString:(int)length {
    NSMutableString *result = [[NSMutableString alloc]init];
    while (result.length != length) {
        NSMutableData* data = [NSMutableData dataWithLength:1];
        SecRandomCopyBytes(kSecRandomDefault, 1, [data mutableBytes]);
        Byte currentChar = 0;
        [data getBytes:&currentChar length:1];
        NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        if (currentChar > ASCII_START_NUMERS && currentChar < ASCII_END_NUMERS) { // 0 to 0
            [result appendString:s];
            continue;
        }
        if (currentChar > ASCII_START_LETTERS_A && currentChar < ASCII_END_LETTERS_Z) { // A to Z
            [result appendString:s];
            continue;
        }
        if (currentChar > ASCII_START_LETTERS_a && currentChar < ASCII_END_LETTERS_z) { // a to z
            [result appendString:s];
            continue;
        }
    }
    return result;
}