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

  • 相关阅读:
    堆栈(线性表)
    链表 -- 循环链表(线性表)
    链表 -- 单向链表(线性表)
    排序算法--归并算法(强分治)
    sqlhelper
    sqlite与sqlserver区别
    常用sql集锦
    外出实施问题总结
    JS深入理解系列(一):编写高质量代码
    通用分页(Jquery版)
  • 原文地址:https://www.cnblogs.com/huntaiji/p/3450206.html
Copyright © 2020-2023  润新知