ios 中数组,字典 集合部类的几种常用枚举方法
展开全部
NSMutableArray *array=[[NSMutableArray alloc]initWithObjects:@apple,@ab,@aa,@aac,@appd, nil];//排序[array sortUsingComparator:^NSComparisonResult(__strong id obj1,__strong id obj2){
NSString *str1=(NSString *)obj1;
NSString *str2=(NSString *)obj2;
return [str1 compare:str2];}];NSLog(@array=%@,array);//枚举字典NSNumber *age=[NSNumber numberWithInt:51];
NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@Anthony,@FirstName,@Robbins,@LastName,age,@age, nil];
[dic enumerateKeysAndObjectsUsingBlock:^(__strong id key,__strong id value,BOOL *stop){
NSLog(@Key=%@,Value For Key=%@,key,value);}];//法二NSEnumerator *keys=[dic keyEnumerator];
id keyInDic=nil;
while ((keyInDic =[keys nextObject])!=nil) {
id valueForKey=[dic objectForKey:keyInDic];
NSLog(@Key=%@,ValueForKey=%@,keyInDic,valueForKey);}//NSSetNSString *hisName=@li;
NSString *hisLastname=@san;
NSString *herName=@zhang;
NSString *herLastname=@san;
NSMutableSet *set=[[NSMutableSet alloc]initWithObjects:hisName,hisLastname,herName,herLastname, nil];
NSLog(@%@,set);//删除对象[set removeObject:herLastname];
NSLog(@%@,set);//添加[set addObject:hisLastname];
NSLog(@%@,set);
[set addObjectsFromArray:array];
NSLog(@%@,set);//遍历set[set enumerateObjectsUsingBlock:^(__strong id objc1,BOOL *stop){
if ([objc1 isKindOfClass:[NSString class]]) {
NSString *str=(NSString*)objc1;
if ([str isEqualToString:@san1]) {
NSLog(@find san in set);*stop=YES;}}}];
//set anyObject
NSString *str1=(NSString *)obj1;
NSString *str2=(NSString *)obj2;
return [str1 compare:str2];}];NSLog(@array=%@,array);//枚举字典NSNumber *age=[NSNumber numberWithInt:51];
NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@Anthony,@FirstName,@Robbins,@LastName,age,@age, nil];
[dic enumerateKeysAndObjectsUsingBlock:^(__strong id key,__strong id value,BOOL *stop){
NSLog(@Key=%@,Value For Key=%@,key,value);}];//法二NSEnumerator *keys=[dic keyEnumerator];
id keyInDic=nil;
while ((keyInDic =[keys nextObject])!=nil) {
id valueForKey=[dic objectForKey:keyInDic];
NSLog(@Key=%@,ValueForKey=%@,keyInDic,valueForKey);}//NSSetNSString *hisName=@li;
NSString *hisLastname=@san;
NSString *herName=@zhang;
NSString *herLastname=@san;
NSMutableSet *set=[[NSMutableSet alloc]initWithObjects:hisName,hisLastname,herName,herLastname, nil];
NSLog(@%@,set);//删除对象[set removeObject:herLastname];
NSLog(@%@,set);//添加[set addObject:hisLastname];
NSLog(@%@,set);
[set addObjectsFromArray:array];
NSLog(@%@,set);//遍历set[set enumerateObjectsUsingBlock:^(__strong id objc1,BOOL *stop){
if ([objc1 isKindOfClass:[NSString class]]) {
NSString *str=(NSString*)objc1;
if ([str isEqualToString:@san1]) {
NSLog(@find san in set);*stop=YES;}}}];
//set anyObject
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询