Objective-C NSDictionary和NSMutaleDictionary用法

这两个类类似于Java中的map,可以为Object定义一个key,通过key来查询对应的Object


#import 

int main (int argc, const char * argv[])
{
    
    @autoreleasepool {
		NSDictionary *dic1=[NSDictionary dictionaryWithObjectsAndKeys:@"Object1",@"key1",@"Object2",@"key2",nil];
		for(NSString *key in dic1)
		{
			NSLog(@"key=%@,Object=%@",key,[dic1 objectForKey:key]);
		}
		 
		NSMutableDictionary *dic2=[NSMutableDictionary dictionary];
		[dic2 setObject:@"object3" forKey:@"key3"];
		[dic2 setObject:@"object4" forKey:@"key4"];
		//可以自行增加object
		
		for(NSString *key in dic2)
		{
			NSLog(@"key=%@,Object=%@",key,[dic2 objectForKey:key]);
		}
		
		
    }
    return 0;
    
}

© 2012, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记