iphone - Need help creating .plist in app -
my app ships .plist looks this:
i want user able add custom exercisename.
so need create new .plist in user's document folder mimics format. can me this?
i need (pseudo code)
if (userdata == nil) { create .plist file; setup .plist mimic format of img above. } save exercisename appropriately.
update:
if (exercisearray == nil) { nsstring *path = [[nsbundle mainbundle]pathforresource:@"data" oftype:@"plist"]; nsmutablearray *rootlevel = [[nsmutablearray alloc]initwithcontentsoffile:path]; self.exercisearray = rootlevel; [rootlevel release]; }
what want load plist nsdictionary, , encode nsdictionary plist file in applications document folder. in applicationdidfinishloading: method, this:
nsstring * documentfile = [nshomedirectory() stringbyappendingformat:@"/documents/myplist.plist"]; if (![[nsfilemanager defaultmanager] fileexistsatpath:documentfile]) { // create copy of our resource nsstring * respath = [[nsbundle mainbundle] pathforresource:@"myplist" oftype:@"plist"]; // note: replace @"myplist" name of file in resources folder. nsdictionary * dictionary = [[nsdictionary alloc] initwithcontentsoffile:respath]; [dictionary writetofile:documentfile atomically:yes]; [dictionary release]; }
then, when want add item, want use nsmutabledictionary modify , save existing plist in app's documents directory:
- (void)addexercise { nsmutabledictionary * changeme = [[nsmutabledictionary alloc] initwithcontentsoffile:documentfile]; ... make changes ... [changeme writetofile:documentfile atomically:yes]; [changeme release]; }
to make changes, need find sub-dictionary containing array of exercises. use setobject:forkey: method on nsmutabledictionary set new array containing new list of exercises. might this:
nsmutablearray * list = [nsmutablearray arraywitharray:[changeme objectforkey:@"list"]]; nsarray * exercises = [[list objectatindex:10] objectforkey:@"exercises"]; nsdictionary * newexercise = [nsdictionary dictionarywithobject:@"type lot" forkey:@"exercisename"]; exercises = [exercises arraybyaddingobject:newexercise]; nsmutabledictionary * dict = [nsmutabledictionary dictionarywithdictionary:[list objectatindex:10]]; [dict setobject:exercises forkey:@"exercises"]; [list replaceobjectatindex:10 withobject:dict]; [changeme setobject:list forkey:@"list"];
once make change, important remember write changeme
plist file in documents directory.
Comments
Post a Comment