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);