How to save data , in an iOS application? -


i developing application user creates event has 3 fields:

category , name , event. after user gives entry , have save button save data future reference. when opens app again data shown in table view.

how "save" data on ios ? know nsuserdefaults , pretty sure not way example.

what ve done far :

i created "note" class category , name , event.

the code save button looks this:

- (ibaction)save:(id)sender {      //creating new "note" object     note *newnote = [[note alloc]init];      newnote.category = categoryfield.text;     newnote.name = namefield.text;     newnote.event = eventfield.text;      // whatever fill object data      nsdata* data = [nskeyedarchiver archiveddatawithrootobject:newnote];      /*      create path documents directory app      */      nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory,                                                          nsuserdomainmask, yes);     nsstring *documentsdirectory = [paths objectatindex:0];      /*      here append unique filename object, in case, 'note'      */      nsstring* filepath = [documentsdirectory stringbyappendingstring:@"note"];      /*      finally, let's write data our file      */      [data writetofile:filepath atomically:yes];      /*      we're done!      */ } 

is correct way save event? how can retrieve wrote ?

secondly if run code again ll overwrite data , or create new entry?

i see how can make new entry every time.

also delete event table presenting them , see how delete work.

my "note" object looks that:

@interface note : nsobject <nscoding> {     nsstring *category;     nsstring *name;     nsstring *event; }  @property (nonatomic, copy) nsstring *category;  @property (nonatomic, copy) nsstring *name;  @property (nonatomic, copy) nsstring *event;  @end 

try

//note.h   #define knotecategory  @"category" #define knotename      @"name" #define knoteevent     @"event"  @interface note : nsobject  @property (nonatomic, copy) nsstring *category; @property (nonatomic, copy) nsstring *name; @property (nonatomic, copy) nsstring *event;  - (id)initwithdictionary:(nsdictionary *)dictionary;  + (nsarray *)savednotes; - (void)save; 

//note.m file

- (id)initwithdictionary:(nsdictionary *)dictionary {     self = [super init];     if (self)     {         self.category = dictionary[knotecategory];         self.name = dictionary[knotename];         self.event = dictionary[knoteevent];     }      return self; }  + (nsstring *)usernotesdocumentpath {     nsstring *documentspath  = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes)[0] stringbyappendingpathcomponent:@"usernotes.plist"];      return documentspath;  }  + (nsarray *)savednotes {     nsstring *documentspath = [self usernotesdocumentpath];     nsarray *savednotes = [nsarray arraywithcontentsoffile:documentspath];     nsmutablearray *savedusernotes = [@[] mutablecopy];     (nsdictionary *dict in savednotes) {         note *note = [[note alloc]initwithdictionary:dict];         [savedusernotes addobject:note];     }      return savedusernotes;  }  - (nsdictionary *)usernotedictionary {     nsmutabledictionary *dict = [nsmutabledictionary dictionary];      if (self.category) {         dict[knotecategory] = self.category;     }     if (self.name) {         dict[knotename] = self.name;     }     if (self.event) {         dict[knoteevent] = self.event;     }      return dict; }  - (void)saveusernotestoplist:(nsarray *)usernotes {     nsmutablearray *mutableusernotes = [@[] mutablecopy];     (note *note in usernotes) {         nsdictionary *dict = [note usernotedictionary];         [mutableusernotes addobject:dict];     }     nsstring *documentspath  = [note usernotesdocumentpath];     [mutableusernotes writetofile:documentspath atomically:yes]; }  #pragma mark - save  - (void)save {     nsmutablearray *savednotes = [[note savednotes] mutablecopy];     [savednotes addobject:self];     [self saveusernotestoplist:savednotes]; } 

save note

- (ibaction)save:(id)sender {      //creating new "note" object     note *newnote = [[note alloc]init];      newnote.category = categoryfield.text;     newnote.name = namefield.text;     newnote.event = eventfield.text;      //saves note plist     [newnote save];      //to saved notes     nsarray *savednotes = [note savednotes]; } 

source code


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -