zoukankan      html  css  js  c++  java
  • NSEnumerator

    集合类(如:NSArray、NSSet、NSDictionary等)均可获取到NSEnumerator, 该类是一个抽象类,没有用来创建实例的公有接口。NSEnumerator的nextObject方法可以遍历每个集合元素,结束返回nil,通过与while结合使用可遍历集合中所有项。

    例子中使用了与前例相同的Photo对象,具体定义参考 隐式循环 这一节。

    #import <Foundation/Foundation.h>
    #import "Photo.h"
    
    int main (int argc, const char * argv[])
    {
    
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        //while+NSEnumerator
        
        //定义对象的数组
        NSArray *array = [NSArray arrayWithObjects:[[Photo alloc] init], [[Photo alloc] init],[[Photo alloc] init], nil];
        
        //通过objectEnumberator获取集合的NSEnumerator
        NSEnumerator *myEnumerator = [array objectEnumerator];
        
        Photo *photo;  
        //nextObject遍历每一项,结束返回nil
        //注意这里使用的是“=”号,所以最外面还要再添加一对()
        while((photo = [myEnumerator nextObject]))
        {
            [photo draw];
        }
    
        [pool drain];
        return 0;
    }
    

    NSSet allObjects

    #import <Foundation/Foundation.h>
    #import "Photo.h"
    
    int main (int argc, const char * argv[])
    {
    
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        //NSSet  allObjects
        NSSet *set = [NSSet setWithObjects:[[Photo alloc] init], [[Photo alloc] init],[[Photo alloc] init], nil];
        
        //allObjects仅能获取NSArray,需要再次调用objectEnumberator
        //并且获取的数组顺序不确定。
        NSEnumerator *myEnumerator = [[set allObjects] objectEnumerator];
        
        Photo *photo;
        while((photo = [myEnumerator nextObject]))
        {
            [photo draw];
        }
    
        [pool drain];
        return 0;
    }
    

    NSDictionary allValues allKeys

    #import <Foundation/Foundation.h>
    #import "Photo.h"
    
    int main (int argc, const char * argv[])
    {
    
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        //NSDictionary allValues
        //NSDictionary 添加项时是value在前j,key在后的,与C#相反
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                             [[Photo alloc] init], @"p1",
                             [[Photo alloc] init], @"p2",
                             [[Photo alloc] init], @"p3", nil];
        
        //allValues 只能获取字典中值的数组列表,注意列表是无序的。
        NSEnumerator *myEnumerator = [[dict allValues] objectEnumerator];
        
        Photo *photo;
        while((photo = [myEnumerator nextObject]))
        {
            [photo draw];
        }
        
        //allKeys 遍历字典中的所有key列表,然后能过objectForKey获取值。注意列表是无序的。
        myEnumerator = [[dict allKeys] objectEnumerator];
        NSString *key;
        while((key = [myEnumerator nextObject]))
        {
            //objectForKey从字典中获取key对应的值
            [[dict objectForKey:key] draw];
        }
        
    
        [pool drain];
        return 0;
    }
    
  • 相关阅读:
    阻止元素默认行为
    微信小程序--页面的生命周期和参数传递
    微信小程序-查询快递
    小程序-冒泡事件
    SpringMVC-使用、运行流程、配置文件寻找
    OpenCV-安装使用、图像处理
    Spring-AOP:JoinPoint、各种通知、基于XML和注解的AOP、声明式事务
    Spring-AOP:开发准备、初识动态代理、使用步骤、
    Spring-IOC:Bean的作用域、生命周期、XML的装配、注解注入、@Autowired
    Spring-IOC:复杂值注入、各种类型赋值、bean的复用
  • 原文地址:https://www.cnblogs.com/chenjunbiao/p/2042853.html
Copyright © 2011-2022 走看看