2011-10-12 31 views
0

我想以编程方式将新行添加到我的pList文件在iPhone上。但我无法找出如何。 我要添加键值:someKey /类型:字符串/ someValue中:字符串
添加新的键/字符串的值pList

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"]; 

NSMutableDictionary *plist = [[NSDictionary dictionaryWithContentsOfFile:path] mutableCopy]; 
NSMutableArray *newArray = [[[NSMutableArray alloc] init] autorelease]; 
newArray = [NSArray arrayWithObjects:@"someKey", @"someValue", nil]; 
[plist setObject:newArray forKey:@"someKey"]; 
[plist writeToFile:path atomically:YES]; 
[plist release]; 

回答

1

在应用程序包,这是上面的代码试图做你不能修改文件。

如果该文件应该是可修改的,您需要先将它移动到文档文件夹中(例如,在第一次运行时),然后随后对其进行读取/写入。有大量的处理如何做到这一点的问题,例如:copying plist to document directory

1

可以使用的NSDictionary的键 - 值编码:

// create dictionary 
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"]; 
// add it to plist 
[plist setObject:dict forKey:@"customDictionary"];