我正在使用此代码将内容添加到plist中:
//////////// Save data into plist /////////////////
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"datatesting.plist"];
NSLog(@"path='%@'",path);
NSFileManager *nfm = [[NSFileManager alloc] init];
if([nfm fileExistsAtPath:path])
{
// if file exists, get its contents, add more entries and write back
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSArray *keys = [NSArray arrayWithObjects:@"Title",@"Description",@"Coordinate",nil];
[array addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@",titlestring],[NSString stringWithFormat:@"%@",descriptionstring],[NSString stringWithFormat:@"%@",coordinadastring], nil] forKeys:keys]];
NSLog(@"modified array=%@",array);
BOOL ok = [array writeToFile:path atomically:YES];
if(!ok){
NSLog(@"Unable to write appended file");
return;
}
} else {
// if file doesn't exist, create a new one
NSMutableArray *array = [[NSMutableArray alloc] init];
NSArray *keys = [NSArray arrayWithObjects:@"Title",@"Description",@"Coordinate",nil];
[array addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@",titlestring],[NSString stringWithFormat:@"%@",descriptionstring],[NSString stringWithFormat:@"%@",coordinadastring], nil] forKeys:keys]];
NSLog(@"new array=%@",array);
BOOL ok = [array writeToFile:path atomically:YES];
if(!ok){
NSLog(@"Unable to write new file");
return;
}
}
现在我在使用plist的内容时遇到了问题。所以我的两个问题是: - 当前plist的词典的键是什么? - 阅读Tableview中内容的方法是什么?
答案 0 :(得分:1)
你有一系列词典。您需要迭代数组的内容,然后查看每个字典。您可以使用块枚举器或较旧的for()样式执行此操作。我现在会使用后者,因为你可能不知道块。
NSArray *array = ...; // read in the array from the file
for(NSDictionary *dict in array) {
NSString *title = [dict objectForKey:@"Title"];
... etc
}
如果要在tablview中显示此数据,则行数是字典数(即[数组计数])。如果你只想显示每个单元格的标题,你将获得单元格行号,然后通过[array objectAtIndex:cellRow]获取字典,然后按上面的方式拉出标题。