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
时间: 2024-10-04 03:31:43