ios - Override an existed Property List -
i want override property list specific dictionary.
nsdictionary *plist = [[nsdictionary alloc]initwithcontentsofurl:url]; nsstring *path = [[nsbundle mainbundle] pathforresource:@"routes" oftype:@"plist"]; nsmutabledictionary *lastdict = [[nsmutabledictionary alloc] initwithcontentsoffile:path]; [lastdict setvalue:[plist objectforkey:@"routes"] forkey:@"routes"]; [lastdict writetofile:path atomically:yes];
ps: plist (dictionary ok) after writetofile method, nothing happend property list path ...
files added main bundle cannot modified (they supposed static), that's reason why code load plist file won't able overwrite it.
you not noticing write operation failing because not checking result. (- writetofile:atomically:
returns bool
tells whether operation completed or not.)
if want have plist file can dynamically edit, should add document folder of app. here sample code show basics of how possible create , edit plist file inside documents.
nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *plistpath = [[paths objectatindex:0] stringbyappendingpathcomponent:@"simple.plist"]; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; bool fileexists = [filemanager fileexistsatpath:plistpath]; if (!fileexists) { // plist file not exist yet have create nslog(@"the .plist file exists"); // create dictionary represents data want save nsdictionary *plist = @{ @"key": @"value" }; // write dictionary disk [plist writetofile:plistpath atomically:yes]; nslog(@"%@", plist); } else { // .plist file exists can interesting stuff nslog(@"the .plist file exists"); // start loading memory nsmutabledictionary *plist = [[nsmutabledictionary alloc] initwithcontentsoffile:plistpath]; nslog(@"%@", plist); // edit [plist setvalue:@"something" forkey:@"key"]; // save [plist writetofile:plistpath atomically:yes]; nslog(@"%@", plist); }
i hope helps!
Comments
Post a Comment