Objective-C字典使用详解

来源:互联网 发布:ubuntu开发c语言 编辑:程序博客网 时间:2024/06/10 03:08
#import <UIKit/UIKit.h>#import "AppDelegate.h"int main(int argc, char * argv[]) {    //类似Java中的Map,即键值对;  NSDictionary *dict = @{@"name":@"zhangsan",@"age":@23};  //打印出整个字典;  NSLog(@"%@",dict);    //取出字典中的某个值;  NSLog(@"%@",[dict objectForKey:@"name"]);    //读取一个plist字典;  //plist文件可以作为配置文件,可以存储数组或者字典。类似Android中的XML配置文件。需要好好使用!  NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]];  NSLog(@"Plist文件中的字典:%@",plistDict);  NSLog(@"Plist中的年龄:%@",[plistDict objectForKey:@"age"]);    }


输出结果如下:

2015-09-22 10:50:32.067 MutableArrayDemo[90943:3898163] {
    age = 23;
    name = zhangsan;
}
2015-09-22 10:50:32.069 MutableArrayDemo[90943:3898163] zhangsan
2015-09-22 10:50:32.082 MutableArrayDemo[90943:3898163] Plist文件中的字典:{
    age = 25;
    name = Jack;
}
2015-09-22 10:50:32.082 MutableArrayDemo[90943:3898163] Plist中的年龄:25



github主页:https://github.com/chenyufeng1991  。欢迎大家访问!

1 1