本帖最后由 枫煠 于 2014-12-29 21:27 编辑
NSArray、NSDictionary各有四种方式- Statement
- //NSArray
- NSArray* arrayItems = @[@1,@2,@3,@4,@5,@6,@7,@8,@9,@10];
- for (int i = 0; i < [arrayItems count]; ++i) {
- NSNumber* number = [arrayItems objectAtIndex:i];
- }
- //NSDictionary
- NSDictionary* dicItems = @{@1:@"1",@2:@"2",@3:@"3",@4:@"4",@5:@"5",@6:@"6",@7:@"7"};
- NSArray* keys = [dicItems allKeys];
- for (int i =0; i< [keys count]; ++i) {
- NSNumber* keyNumber = [keys objectAtIndex:i];
- }
复制代码
- For in
- //NSArray
- NSArray* arrayItems = @[@1,@2,@3,@4,@5,@6,@7,@8,@9,@10];
- for (NSNumber* number in arrayItems){
- }
- //NSDictionary
- NSDictionary* dicItems = @{@1:@"1",@2:@"2",@3:@"3",@4:@"4",@5:@"5",@6:@"6",@7:@"7"};
- for (NSNumber* keyNumber in [dicItems allKeys]){
- }
复制代码
- Block
- //NSArray
- NSArray* arrayItems = @[@1,@2,@3,@4,@5,@6,@7,@8,@9,@10];
- [arrayItems enumerateObjectsUsingBlock:^(NSNumber* number, NSUInteger idx, BOOL *stop) {
- }];
- //NSDictionary
- NSDictionary* dicItems = @{@1:@"1",@2:@"2",@3:@"3",@4:@"4",@5:@"5",@6:@"6",@7:@"7"};
- [dicItems enumerateKeysAndObjectsUsingBlock:^(NSNumber* keyNumber, NSString* value, BOOL *stop) {
- }];
复制代码
Enumerator
- //NSArray
- NSArray* arrayItems = @[@1,@2,@3,@4,@5,@6,@7,@8,@9,@10];
- NSEnumerator* enumerator = [arrayItems objectEnumerator];
- NSNumber* number;
- while (number =[enumerator nextObject]) {
- }
- //NSDictionary
- NSDictionary* dicItems = @{@1:@"1",@2:@"2",@3:@"3",@4:@"4",@5:@"5",@6:@"6",@7:@"7"};
- NSEnumerator* enumerator = [dicItems keyEnumerator];
- NSNumber* keyNumber;
- while (keyNumber = [enumerator nextObject]) {
- }
复制代码
这四种方式各有优缺点,另外做一个小Demo比较四种方式搜寻物件的时间
最后得到的结果是NSArray使用Block会比较快一点, NSDictionary用Enumerator比较快,有兴趣的自己去下载看看吧
|
|