1,生成随机字符串
//
for (int i = 0 ; i < 10; i++) {
//arc4random会产生比较大的随机数,然后整除26除余数,即是0~26
//因为小写字母a的c码是97,A的c码是65,加上26个随机数字,产生26个小写字母
//最后要以c输出
int n = arc4random()%26+97;
int m = arc4random()%10;
int n = arc4random()%26+65;
if (arc4random()%2 == 0) {
NSLog(@"%c%d",n,m);
}NSLog(@"%d%c",m,n);
}
2,将随机英文字符串按首字符分类
for (int i = 0 ; i < 1000; i++) {
int n1 = arc4random()%26+97;
int n2 = arc4random()%26+97;
int n3 = arc4random()%26+97;
int n4 = arc4random()%26+97;
int n5 = arc4random()%26+97;
//value
NSString *arcStr = [NSString stringWithFormat:@"%c%c%c%c%c",n1,n2,n3,n4,n5];
//key
char ch = [arcStr characterAtIndex:0];
NSString *lowCh = [NSString stringWithFormat:@"%c",ch];
NSString *bigCh = [lowCh uppercaseString];//转为大写,小写lowercaseString
//检验dic是否有bigCh这个键
NSArray *arrValue = [self.dict1 objectForKey:bigCh];
NSMutableArray *arrMut ;
if (arrValue == nil) {
//用这个字符串把集合创建出来,构建动态数组
arrValue = [NSArray arrayWithObject:arcStr];
arrMut = [NSMutableArray arrayWithArray:arrValue];
}else
{
//构建动态数组,然后将字符串补充到对应字母集合。
//如果添加的是数组arrValue,会在数组arrValue中创建数组arrValue
arrMut = [NSMutableArray arrayWithArray:arrValue];
[arrMut addObject:arcStr];
}
[self.dict1 setObject:arrMut forKey:bigCh];
}
NSLog(@"self.dict1%@",self.dict1);