您的位置:首页 > 移动开发 > Objective-C

Objective-C 字典小结

2012-05-11 12:36 218 查看
//创建字典的方法
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
@"wtq",@"name1",
@"wtm",@"name2",
@"wtp",@"name3",
nil];

//将字典的值输出来
NSLog(@"new dictionary is %@",[dic objectForKey:@"name2"]);

//如何遍历字典;使用枚举
NSEnumerator *e = [dic objectEnumerator];
id ee;
while (ee=[e nextObject]) {
NSLog(@" enumer %@",ee);
}

//获取字典中的所有值

NSArray *values = [dic
allValues];

NSLog(@"all:%@",values);

//保存字典到文件

NSString *filePath = [[[NSBundle
mainBundle]resourcePath]
stringByAppendingPathComponent:@"dict.text"];

BOOL success = [dic writeToFile:filePath
atomically:YES];

//相反,我们可以用一个文件的内容来填充一个字典

NSDictionary *myDict2 = [NSDictionary
dictionaryWithContentsOfFile:filePath];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: