开发者

Adding new key/value of type string to pList

开发者 https://www.devze.com 2023-04-12 05:52 出处:网络
I am trying to add programmatically new row to my pList file on iPhone. But I can\'t find out how. I want to add key: someKey/ type: string/ someValue: string

I am trying to add programmatically new row to my pList file on iPhone. But I can't find out how. I want to add key: someKey/ type: string/ someValue: string

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 atomi开发者_StackOverflowcally:YES];
[plist release];


You can't modify files in the application bundle, which is what your code above is attempting to do.

If the file is supposed to be modifiable, you need to move it into the documents folder first (say, on first run) and then read / write to that one subsequently. There are plenty of questions dealing with how to do this, for example : copying plist to document directory


You can use NSDictionary for key-value coding:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号