NSArray NSSet NSDictionary 元素取出方法

NSArray *array = @[@"123",@"12333333",@"楼主傻逼",@"LZSB",@"顶",@"楼上都是傻逼",@"土豪我们做朋友吧",@"回复LS 滚"];

//for循环
    for(int i=0;i<[array count];i++)
        NSLog(@"%@",[array objectAtIndex:i]);

//for in 快速枚举
    for(NSString *str in array)
        NSLog(@"%@",str);

//迭代
    NSEnumerator  *enumer = [array objectEnumerator];
    
    NSString *strnext =nil;
    
    while((strnext = enumer.nextObject))
    {
        NSLog(@"%@",strnext);
    }
    //blocks 枚举
    [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"%@",obj);
    }];

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"Val_1",@"Key_1",@"Val_2",@"Key_2",nil];
    
    //for
    
    NSArray *keysArray = [dic allKeys];
    
    for(int i = 0 ; i< [dic count]; i++)
    {
        NSLog(@"%@",[dic objectForKey:[keysArray objectAtIndex:i]]);
    }
    
    //for in
    for(NSString *key in keysArray)
        NSLog(@"%@",[dic objectForKey:key]);
    
    //Enumerator
    NSEnumerator *enumerator = [dic keyEnumerator];
    
    NSString *keyEnum = nil;
    
    while((keyEnum = enumerator.nextObject))
        NSLog(@"%@",[dic objectForKey:keyEnum]);
 
  
 
  

你可能感兴趣的:(OC)