码迷,mamicode.com
首页 > 编程语言 > 详细

遍历数组和字典

时间:2015-03-17 21:59:03      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:遍历数组   遍历字典   nsdictionary   nsarray   

1.遍历字典
NSDictionary *dictionary = @{@"name": @"jack", @"age": @"18"};

1>for (NSString *key in dictionary) {
    id obj = dictionary[key];
}

2>NSArray *keyArray = [dictionary allKeys];
NSArray *valueArray = [dictionary allValues];
for (NSString *key in keyArray) {
    id obj = valueArray[key];
}

3>[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    *stop = YES;
}];


2.遍历数组
NSArray *array = @[@"jack", @"lucy"];

1>for (NSString *str in array) {

}

2>[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    
}];

3>迭代器遍历数组
NSEnumerator *enumerator = [array objectEnumerator];
[enumerator allObjects];//有值
id obj = nil;
while (obj = [enumerator nextObject]) {
    
}
[enumerator allObjects];//返回nil

遍历数组和字典

标签:遍历数组   遍历字典   nsdictionary   nsarray   

原文地址:http://blog.csdn.net/u014656271/article/details/44348213

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!