/**
*遍历
*/
#import <Foundation/Foudation.h>
int main(int argc,const char *argv[]){
@autoreleasepool{
NSArray *listOfObjects=[NSArray arrayWithObjects:@"Hello World",@"Bonjour tout le monde",@"Hola Mundo",nil];
NSArray *listOfKeys=[NSArray arrayWithObjects:@"english",@"french",@"spanish",nil];
NSDictionary *dictionary=[NSDictionary dictionaryWithObjects:listOfObjects forKeys:listOfKeys[;
for(NSString *s in [dictionary allKeys]){
NSLog(@"key:%@",s);
}
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key,id obj,BOOL *stop){
NSLog(@"key = %@ and obj = %@",key,obj);
}];
}
return 0;
}
/*
*操纵字典在字典中添加、删除和插入对象。方式使用NSMutableDictionary
*/
//对象的创建:
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
//添加键值对
[dictionary setObject:@"Hello World" forKey:@"english"];
[dictionary setObject:@"Bonjour tout le mode" forKey:@"french"];
[dictionary setObject:@"Hola Mundo forKey:@"spanish"];
[dictionary removeObjectForKey@"french"];
/**
*example
*/
#import <Foundation/Foundation.h>
int main(int argc,const char *argv[]){
@autoreleasepool{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
[dictionary setObject:@"Hello World" forKey:@"english"];
[dictionary setObject:@"Bonjour tout le monde" forKey:@"french"];
[dictionary setObject:@"Hola Mundo" forKey:@"spanish"];
NSLog(@"OBJECT ADDED TO DICTIONARY:%@",dictionary);
[dictionary removeObjectForKey:@"french"];
NSLog(@"OBJECTED REMOVED FROM DICTIONARY%@",dictionary);
[dictionary removeAllObjects];
NSLog(@"ALL OBJECTS REMOVED FROM DICTIONARY:%@",dictionary);
}
return 0;
}
//第一次添加之后结果为:
OBJECT ADDED TO DICTIONARY:{
english="Hello World";
french="Bonjour tout le monde";
spanish="Hola Mundo";
}
//第一次移除了一个元素
OBJECT REMOVED FROM DICTIONARY:{
english ="Hello World";
spanish="Hola Mundo";
}
//最后所有的元素被移除
ALL OBJECTS REMOVED FROM DICTIONARY:{
}
//将字典中的对象保存到文件系统中以供日后或其他程序使用。
//NSDictionary中有 writeToFile可以将字典写到文件里
NSArray *listOfObjects=[NSArray arrayWithObjects:@"Hello World",@"Bonjour tout le monde",@"Hola Mundo",nil];
NSArray *listOfKeys=[NSArray arrayWithObjects:@"english",@"french",@"spanish",nil];
NSDictionary *dictionary=[NSDictionary dictionaryWithObjects:listOfObjects forKeys:listOfKeys];