利用多核优势,遍历NSDictionary

来源:互联网 发布:linux安装输入法命令 编辑:程序博客网 时间:2024/06/04 19:28

在现在多核CPU的时代,如果遍历NSDictionary的时候你还是通过先取key,在通过key取v alue的方式,你就out啦

    NSMutableDictionary        *_dbalertlogMap;[_dbalertlogMap enumerateKeysAndObjectsUsingBlock:^(id key, id obj,   BOOL *stop) { NSLog(@“value for key %@ is %@”, key, value);}
用这种办法可以充分利用CPU性能。

下面再把传统的几个遍历方式总结下:

方法一:

- (void)describeDictionary:(NSDictionary *dict){   NSArray *keys;   int i, count;   id key, value;   keys = [dict allKeys];   count = [keys count];   for (i = 0; i < count; i++)  {  key = [keys objectAtIndex: i];  value = [dict objectForKey: key];  NSLog (@"Key: %@ for value: %@", key, value);  }}
方法二:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];for (NSString *key in dict) { NSLog(@"key: %@ value: %@", key, dict[key]);}
方法三


 NSLog(@"Hello, World!");        NSDictionary *myDic=[[NSDictionary alloc]initWithObjectsAndKeys:@"张三",@"name",@"李四",@"name", nil];                NSUInteger count = [myDic count];          NSLog(@"词典的数量为:  %lu",count);               NSEnumerator * myEnumerator = [myDic keyEnumerator];                           for (NSObject *object in myEnumerator) {              NSLog(@"遍历KEY的值: %@",object);          }                myEnumerator = [[myDic allValues] objectEnumerator];        NSString *value;        while((value = [myEnumerator nextObject]))        {            NSLog(@"遍历的值: %@",value);        }                //通过KEY找到value          NSObject *myObject = [myDic objectForKey:@"name"];                if (myObject != nil) {              NSLog(@"通过KEY找到的value是: %@",myObject);          }                  NSMutableDictionary *mydic2 = [NSMutableDictionary dictionaryWithCapacity:10];          [mydic2 setObject:@"Alex Hu" forKey:@"name"];          [mydic2 setObject:@"1388888888" forKey:@"mobile number"];                 for (NSObject *object in [mydic2 objectEnumerator]) {              NSLog(@"遍历的值: %@",object);          }




0 0
原创粉丝点击