Objective c 将字典项从plist转换为NSArray
我有一个具有以下架构/结构的plist:Objective c 将字典项从plist转换为NSArray,objective-c,ios,dictionary,nsarray,plist,Objective C,Ios,Dictionary,Nsarray,Plist,我有一个具有以下架构/结构的plist: dictionary key1 item1 key2 item2 key3 item3 dictionary ... 我想将每个字典的第一个键(key1)的item(item1)加载到一个NSArray,这样我就可以将NSArray加载到UITableView中 我谨此陈辞: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDi
dictionary
key1
item1
key2
item2
key3
item3
dictionary
...
我想将每个字典的第一个键(key1)的item(item1)加载到一个NSArray,这样我就可以将NSArray加载到UITableView中
我谨此陈辞:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
然后我想添加如下内容:
tablearray = [array objectsForKey:@"key1"];
但这不是命令。似乎我需要一些帮助…:) 苹果提供了一个描述如何从pList中读取和写入pList的工具。你可以通过它
我们将从pList获取数据作为NSData,并使用以下方法将其转换为NSDictionary
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
您可以使用
NSMutableArray * myArray = [NSMutableArray alloc] init];
//temp is the response dictionary
NSArray * myKeys = [temp allKeys];
for (int index = 0; index<[myKeys count]; index++) {
id value = [temp valueForKey:[myKeys objectAtIndex:index]];
[myArray addObject:value];
}
NSMutableArray*myArray=[NSMutableArray alloc]init];
//temp是响应字典
NSArray*myKeys=[temp allKeys];
对于(int index=0;index,您可以在数组上循环并按以下方式添加对象:
NSMutableArray *tablearray = [NSMutableArray arrayWithCapacity:[array count]];
for (NSDictionary *dict in array) {
[tablearray addObject:[dict objectForKey:@"key1"]];
}