ios - Delete from Plist is not working? -


i have plist , when user write notes save plist along id,each time when user opens check whether user id got notes in plist , display in uitableview .also user can remove notes when tried following process got exceptions

1.in view didload check whether user got previous notes or not 2.check plist user id 3.if match retrieve corresponding notes 4.and save mutable array .so when user add new note first use previous mutable array store new note , write again plist //not working me. 5.when user delete notes update itinto plist

i assume have structure similar this

[     {         "userid": 1,         "notes": [             {                 "noteid": 1,                 "desc": "description"             },{                 "noteid": 2,                 "desc": "description"             }         ]     } ] 

plist file path in documents directory

- (nsstring *)usernotesfilepath{     nsstring *documents = nssearchpathfordirectoriesindomains(nsdocumentdirectory,                                                               nsuserdomainmask,                                                               yes)[0];       return [documents stringbyappendingpathcomponent:@"usernotes.plist"];  } 

method fetches saved notes user id

- (nsarray *)savednotesforuserid:(nsinteger)userid{      nsstring *filepath = [self usernotesfilepath];     nsarray *savednotes = [nsarray arraywithcontentsoffile:filepath];     nspredicate *predicate = [nspredicate predicatewithformat:@"userid = %d",userid];      nsdictionary *user = [[savednotes filteredarrayusingpredicate:predicate]lastobject];      return  user[@"notes"]; } 

saves new notes array such particular userid

- (void)insertnotes:(nsarray *)notesarray  foruserid:(nsuinteger)userid{      if (!notesarray) {         return;     }      nsstring *filepath = [self usernotesfilepath];     nsmutablearray *savednotes = [nsmutablearray arraywithcontentsoffile:filepath];      nspredicate *predicate = [nspredicate predicatewithformat:@"userid = %d",userid];      nsinteger index = [savednotes indexofobjectpassingtest:^bool(id obj, nsuinteger idx, bool *stop){         return [predicate evaluatewithobject:obj];     }];      nsmutabledictionary *user = [savednotes[index] mutablecopy];     user[@"notes"] = notesarray;      [savednotes replaceobjectatindex:index withobject:user];     [savednotes writetofile:filepath atomically:yes];  } 

insert 1 note saved notes

- (void)insertnote:(nsdictionary *)usernote  foruserid:(nsuinteger)userid{      if (!usernote) {         return;     }      nsstring *filepath = [self usernotesfilepath];     nsmutablearray *savednotes = [nsmutablearray arraywithcontentsoffile:filepath];      nspredicate *predicate = [nspredicate predicatewithformat:@"userid = %d",userid];      nsinteger index = [savednotes indexofobjectpassingtest:^bool(id obj, nsuinteger idx, bool *stop){         return [predicate evaluatewithobject:obj];     }];      nsmutabledictionary *user = [savednotes[index] mutablecopy];      nsmutablearray *savedusernotes = [user[@"notes"] mutablecopy];     if (!savedusernotes) {         savedusernotes = [nsmutablearray array];     }      [savedusernotes addobject:usernote];      user[@"notes"] = savedusernotes;      [savednotes replaceobjectatindex:index withobject:user];     [savednotes writetofile:filepath atomically:yes]; } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -