Thursday, 15 January 2015

iphone - saving NSMutableArray of object to pList -



iphone - saving NSMutableArray of object to pList -

i'm trying save array of objects plist when home button or programme exited, nil happen , file never seems created.

what doing wrong????

//viewcontroller.h

@interface viewcontroller : uiviewcontroller <uitableviewdatasource, uitableviewdelegate, uialertviewdelegate> @property (weak, nonatomic) iboutlet uitableview *mytableview; @property (strong, nonatomic) nsmutablearray *fieldbooks; - (nsstring *)pathoffile; - (void)applicationwillterminate:(nsnotification *)notification; @end

//viewcontroller.m

#import "viewcontroller.h" @interface viewcontroller () - (void)viewdidload { nsstring *filepath = [self pathoffile]; if ([[nsfilemanager defaultmanager]fileexistsatpath:filepath]) { nsarray *array = [[nsarray alloc]initwithcontentsoffile:filepath]; fieldbooks = [array objectatindex:0]; } uiapplication *app = [uiapplication sharedapplication]; [[nsnotificationcenter defaultcenter]addobserver:self selector:@selector(applicationwillterminate:) name:uiapplicationwillterminatenotification object:app]; [super viewdidload]; //instantiate our nsmutablearray //set title self.title = @"fieldbooks"; //add edit button self.navigationitem.leftbarbuttonitem = self.editbuttonitem; //add add together button uibarbuttonitem *addbutton = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemadd target:self action:@selector(insertnewobject)]; self.navigationitem.rightbarbuttonitem = addbutton; } - (void)setediting:(bool)editing animated:(bool)animated { [super setediting:editing animated:animated]; [mytableview setediting:editing animated:animated]; } - (void)insertnewobject { uialertview * alert = [[uialertview alloc] initwithtitle:@"enter name" message:@"" delegate:self cancelbuttontitle:@"cancel" otherbuttontitles:@"ok", nil]; alert.alertviewstyle = uialertviewstyleplaintextinput; [alert show]; } - (nsstring *)pathoffile { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentationdirectory, nsuserdomainmask, yes); nsstring *documentsfolder = [paths objectatindex:0]; homecoming [documentsfolder stringbyappendingformat:@"myfile.plist"]; } - (void)applicationwillterminate:(nsnotification *)notification { nsmutablearray *array = [[nsmutablearray alloc]init]; [array addobject:fieldbooks]; [array writetofile:[self pathoffile] atomically:yes]; }

nsdocumentationdirectory

hm. i'm sure meant

nsdocumentdirectory

instead.

iphone nsmutablearray plist

No comments:

Post a Comment