默认的情况下,要读取plist文件的数据是用NSDictionary或者其子类NSMutableDictionary。但有时这样读取很不方便,更希望是用NSArray或者其子类NSMutableArray接受plist文件数据。那可以进行以下操作,现总结如下:
创建一个plist文件,名字是student。右键add row 加入行。
选中plist文件,右键 》 open as 》 source code,这样plist文件会以xml格式呈现,修改、删除其中的值。
现在用数组接受plist文件数据。
NSString *fielPath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];
NSArray *students = [NSArray arrayWithContentsOfFile:fielPath];
for (NSDictionary *stu in students) {
NSLog(@"num = %@, name = %@", [stu objectForKey:@"num"], [stu objectForKey:@"name"]);
}
结果: