zoukankan      html  css  js  c++  java
  • NSArray,NSSet,NSDictionary的遍历,基本使用集锦

    更多技术性文章请关注 合伙呀 

    NSArray *array = [NSArray arrayWithObjects:@"zhangsan",@"lisi",@"wangwu",@"zhaoda", nil];
            //如何把  数组元素 一一取出?
            //方法一:for循环
            for (int i = 0; i < [array count]; i++) {
                NSString *obj = [array objectAtIndex:i];
                NSLog(@"%@",obj);
            }
            NSLog(@"----------");
            //方法二:使用枚举器NSEnumerator
            NSEnumerator *enumerator = [array reverseObjectEnumerator];
            NSString *obj = nil;
            while (obj = [enumerator nextObject]) {
                NSLog(@"obj = %@",obj);
            }
            NSLog(@"----------");

            //方法三:快速枚举 forin
            for (NSString *obj in array) {
                NSLog(@"%@",obj);
            }
            NSLog(@"----------");
            //方法四:使用专业的枚举方式(blocks等)
            [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                NSLog(@"!!!%@ %lu",obj,idx);
                if(idx == 1)
                {
                    *stop = YES;
                }
            }];
            
            NSLog(@"#########");
            
            //字典四种方法同上!
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"cui",@"name",@"18",@"age",@"nan",@"gender", nil];
            //如何把  字典元素  一一取出?//字典没有下标概念  只能靠key区分对象
            NSArray *allKeys = [dic allKeys];
            NSArray *allValue = [dic allValues];
            for(int i = 0;i < [dic count];i++){
                NSString *key = [allKeys objectAtIndex:i];
                //NSString *obj =[allValue objectAtIndex:i];
                NSString *obj = [dic objectForKey:key];
                NSLog(@"key = %@ obj = %@",key,obj);
            }
            
            
            NSEnumerator *en = [dic keyEnumerator];
            NSString *key = nil;
            while (key = [en nextObject]) {
                NSLog(@"%@ = %@",key,[dic objectForKey:key]);
            }
            for (NSString *key in dic) {
                NSLog(@"-%@--%@",key,[dic objectForKey:key]);
            }

            
            
            NSLog(@"++++++++++++");
            NSSet *set = [NSSet setWithObjects:@"guangmu",@"duowen",@"zengchang",@"chiguo", nil];
            //如何把  集合元素 一一取出?
            NSArray *objects = [set allObjects];
            for (int i = 0; i < [objects count]; i++) {
                NSLog(@"%@",[objects objectAtIndex:i]);
            }
            
            NSEnumerator *e = [set objectEnumerator];
            NSString *o = nil;
            while (o = [e nextObject]) {
                NSLog(@"%@",o);
            }
            for (NSString *obj in set) {
                NSLog(@"=%@",obj);
            }
            
        }

  • 相关阅读:
    linux 查看磁盘空间大小
    Redis内存碎片率
    redis的incr和incrby命令
    redis如何清空当前缓存和所有缓存
    ArcGIS矢量数据批量合并工具
    arcgis 获得工具有多少个
    GoogleEarth二次开发难点和技巧
    ArcGIS 智能批量赋高程工具
    arcgis python支持汉字
    ArcGIS 宗地图批量打印输出
  • 原文地址:https://www.cnblogs.com/huntaiji/p/3450206.html
Copyright © 2011-2022 走看看