objective c - How to edit elements in plist file -
i have settings.plist , want edit values in file.
my function edit/writing is:
- (void) setparamwithname: (nsstring*) name withvalue: (nsstring*) value { // paths root direcory nsarray *paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); // documents path nsstring *documentspath = [paths objectatindex:0]; // path plist file nsstring *plistpath = [documentspath stringbyappendingpathcomponent:@"settings.plist"]; // check see if data.plist exists in documents if (![[nsfilemanager defaultmanager] fileexistsatpath:plistpath]) { // if not in documents, property list main bundle plistpath = [[nsbundle mainbundle] pathforresource:@"settings" oftype:@"plist"]; } // read property list memory nsdata object nsdata *plistxml = [[nsfilemanager defaultmanager] contentsatpath:plistpath]; nsstring *errordesc = nil; nspropertylistformat format; // convert static property list dictionary object nsdictionary *temp = (nsdictionary *)[nspropertylistserialization propertylistfromdata:plistxml mutabilityoption:nspropertylistmutablecontainersandleaves format:&format errordescription:&errordesc]; if (!temp) { nslog(@"error reading plist: %@, format: %d", errordesc, format); } // checking if element exists, if yes overwriting // if element not exists adding new element [temp writetofile:plistpath atomically:yes];
}
this function read , write (with te same values) settings.plist.
i not have idea (my knowledge objective-c not enough) how add new element or edit existing element. can mi issue?
i think it's easier think.
once got path of file read nsdictionary. make mutable copy of dictionary mutablecopy , nsmutabledictionary. edit mutable dictionary (add s.th., remove s.th., edit s.th. , on).
now you're done can write old path did temp.
your main problem you're not working mutable version of dicitionary. it'd make life easier.
Comments
Post a Comment