objective c - Appending some data to a plist -
i'm creating app allows log board game plays. i'm storing logs in plist , i'm trying figure out how append plist. think understand logic i'm having trouble putting code.
my plist looks so:
right i'm trying believe overwrite plist file instead of appending
//get path root directory nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask, yes); nslog(@"paths: %@",paths); //get path documents directory nsstring *documentspath = [paths objectatindex:0]; nslog(@"documentspath: %@",documentspath); //get path logs.plist file nsstring *plistpath = [documentspath stringbyappendingpathcomponent:@"logs.plist"]; //create dictionary values nsdictionary *plistdict = [nsdictionary dictionarywithobjects:[nsarray arraywithobjects:mylogtitle, name, players, mynotes, nil] forkeys:[nsarray arraywithobjects:@"log title",@"name",@"players","notes", nil]]; //write out data [plistdict writetofile:plistpath atomically:yes];
any appreciated.
try this
nsstring *path = [[nsbundle mainbundle] pathforresource: @"data" oftype: @"plist"]; nsdictionary *dict = [nsdictionary dictionarywithcontentsoffile: path]; nslog(@"dictbeforeappending..%@",dict); //getting previous array..... nsmutablearray *prevarray = [[nsmutablearray alloc]init]; prevarray = [[dict valueforkey:@"root"] valueforkey:@"logs"]; nslog(@"prevarray..%@",prevarray); // add new stuff old stuff..... nsmutablearray *players =[[nsmutablearray alloc]initwithobjects:@"newplayer1",@"newplayer2",@"newplayer3", nil];//1 nsdictionary *newobject = [nsdictionary dictionarywithobjects:[nsarray arraywithobjects:@"logtiltle2", @"krish", players, @"it more fun", nil] forkeys:[nsarray arraywithobjects:@"log title",@"name",@"players",@"notes", nil]];//2 nsmutablearray *newarray = [[nsmutablearray alloc]init]; [newarray addobject:newobject]; (int i=0;i<[prevarray count];i++){ [newarray addobject:[prevarray objectatindex:i]]; } //set values plist...... nsmutabledictionary *allitemsdict = [[nsmutabledictionary alloc]init]; nsmutablearray *logs = [[nsmutablearray alloc]initwitharray:newarray]; [allitemsdict setobject:logs forkey:@"logs"]; nsmutabledictionary *root = [[nsmutabledictionary alloc]init]; [root setobject:allitemsdict forkey:@"root"]; // now, write plist: [root writetofile:path atomically:yes]; nsdictionary *dictafterappending = [nsdictionary dictionarywithcontentsoffile: path]; nslog(@"dictafterappending..%@",dictafterappending);
if stuck @ anywhere please let me know dude...
Comments
Post a Comment