how to edit a plist programmatically in xcode 5 iOS 7?

不打扰是莪最后的温柔 提交于 2019-12-06 12:25:19

All you have to do is to use NSMutableDictionary instead of a regular dictionary to modify stuff. You have to always check that the object you are editing is there.

To get the right path for the plist you created in Xcode you need to use:

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

You might actually want to save that file into the app's document directory. So you would use the following path for saving the content:

NSString *savingPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
savingPath = [savingPath stringByAppendingPathComponent:@"Modified.plist"];
[Dict writeToFile:savingPath atomically:YES];

Well, in your case the solution would be the following (when you have Dictionary as a member of Array):

NSString *path = [[NSBundle mainBundle] pathForResource:@"List" ofType:@"plist"];
NSArray *newContent = [[NSArray alloc]initWithContentsOfFile:path];
//iterating through all members since all of them has the string "YES" which should be replaced, otherwise, you will need to create a separate dictionary for each member
for ((i = 0; i < [newContent count]; i++)){
//here you take the member (dictionary) of the array
    NSDictionary *dictOfArray = [newContent objectAtIndex:i];
    [dictOfArray objectForKey:@"Enabled"]=@"NO";
    }
//if you update the same pList you can use the same path (otherwise use another path)
[newContent writeToFile:path atomically:YES];

So, it works now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!