读取一个.plist文件
P 记得是CS193P里的Presence2吧,需要读取一个plist作为数据源。
以下就是一个简单的读取plist到NSDictionary或NSArray的代码。
NSBundle *bundle = [NSBundle mainBundle]; //取得mainBundle NSString *plistPath = [bundle pathForResource:@"文件名" ofType:@"plist"]; //取得文件路径 // 或可以写成 NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"文件名" ofType:@"plist"]; //读取到一个NSDictionary NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; //读取到一个NSArray NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath]; |
一个小提醒,读出来的可能是复杂对象,比如twitter的恶心json文件