ios - Adding dictionaries to Plist programmatically -
ios - Adding dictionaries to Plist programmatically -
i trying add together dictionary plist programmatically in next format:
root(dict) | stringofviewid(dict) | buttontitle(dict) | string string
i can want maintain adding viewid(dict) more buttontitle(dict) under same viewid(dict).
so far can replace existing.
something this:
root(dict) | stringofviewid(dict) - buttontitle(dict)(2)-string string | buttontitle(dict)(1) | string string
here code i'm using:
//initialize , load plist file here: [...] nsmutabledictionary *data; nsmutabledictionary *viewid; nsmutabledictionary *buttonname; nsarray *keys; nsarray *locations; // insert info plist nsnumber *xnumber = [[nsnumber alloc] initwithdouble:locationx]; nsnumber *ynumber = [[nsnumber alloc] initwithdouble:locationy]; keys = [nsarray arraywithobjects:@"locationx", @"locationy", nil]; locations = [nsarray arraywithobjects:xnumber, ynumber, nil]; info = [nsmutabledictionary dictionarywithobjects:locations forkeys:keys]; buttonname = [nsmutabledictionary dictionarywithobject:data forkey:mybuttontitle]; viewid = [nsmutabledictionary dictionarywithobject:buttonname forkey:@"stringofviewid"]; [viewid writetofile:path atomically:yes];
thanks
i create class serve info model. simple implementation - cleaner create button
object rather pass multiple parameters , retrieve dictionary
viewbuttons.h
@interface viewbuttons : nsobject +(viewbuttons *) viewbuttonswithcontentsoffile:(nsstring *)file; -(void) addbutton:(nsstring *)buttonname withx:(double) x andy:(double) y toview:(nsstring *)viewname; -(nsarray *)viewnames; -(nsarray *)buttonnamesforview:(nsstring *)viewname; -(nsdictionary *)buttonwithname:(nsstring *)name inview:(nsstring *)viewname; -(void)writetofile:(nsstring *)file; @end
viewbuttons.m
#import "viewbuttons.h" @interface viewbuttons () @property (nonatomic,strong) nsmutabledictionary *viewbuttons; @end @implementation viewbuttons -(id) init { if (self=[super init]) { self.viewbuttons=[nsmutabledictionary new]; } homecoming self; } +(viewbuttons *) viewbuttonswithcontentsoffile:(nsstring *)file { viewbuttons *newviewbuttons=[viewbuttons alloc]; newviewbuttons.viewbuttons=[nsmutabledictionary dictionarywithcontentsoffile:file]; homecoming newviewbuttons; } -(void) addbutton:(nsstring *)buttonname withx:(double) x andy:(double) y toview:(nsstring *)viewname { nsmutabledictionary *viewdict=self.viewbuttons[viewname]; if (viewdict == nil) { viewdict=[nsmutabledictionary new]; self.viewbuttons[viewname]=viewdict; } else if (![viewdict iskindofclass:[nsmutabledictionary class]]) { viewdict=[viewdict mutablecopy]; self.viewbuttons[viewname]=viewdict; } nsnumber *xnumber = [nsnumber numberwithdouble:x]; nsnumber *ynumber = [nsnumber numberwithdouble:y]; nsdictionary *buttondict=@{@"locationx":xnumber,@"locationy":ynumber}; viewdict[buttonname]=buttondict; } -(nsarray *)viewnames { homecoming self.viewbuttons.allkeys; } -(nsarray *)buttonnamesforview:(nsstring *)viewname { homecoming [self.viewbuttons[viewname] allkeys]; } -(nsdictionary *)buttonwithname:(nsstring *)name inview:(nsstring *)viewname { homecoming self.viewbuttons[viewname][name]; } -(void)writetofile:(nsstring *)file { [self.viewbuttons writetofile:file atomically:yes]; } @end
you can utilize class follows -
viewbuttons *viewbuttons=[viewbuttons viewbuttonswithcontentsoffile:buttonfile]; if (viewbuttons == nil) { viewbuttons=[viewbuttons new]; } [viewbuttons addbutton:@"mybutton1" withx:0 andy:0 toview:@"myview"]; [viewbuttons addbutton:@"mybutton2" withx:1 andy:1 toview:@"myview"]; [viewbuttons addbutton:@"mybutton3" withx:0 andy:0 toview:@"mysecondview"]; [viewbuttons addbutton:@"mybutton4" withx:0 andy:1 toview:@"mythirdview"]; [viewbuttons writetofile:buttonfile];
ios nsdictionary plist
Comments
Post a Comment