objective c - Cannot change mutable array? -
ok have been stuck on while though it's simple problem, trying add nsdictionary array when calling addobject method on array program crashes claiming sending mutating method immutable object.
my code looks like:
- (ibaction)btnsavemessage:(id)sender { //save message , clear text fields nsmutabledictionary *newmessagedictionary = [[nsmutabledictionary alloc] init]; [newmessagedictionary setobject:@"plist test title" forkey:@"title"]; [newmessagedictionary setobject:@"plist subtitle" forkey:@"subtitle"]; [newmessagedictionary setobject:@"-3.892119" forkey:@"longitude"]; [newmessagedictionary setobject:@"54.191707" forkey:@"lattitude"]; nsmutablearray *messagesarray =[[nsmutablearray alloc]init]; //load plist array nsstring *messagespath = [[nsbundle mainbundle] pathforresource:@"messages" oftype:@"plist"]; messagesarray = [nsarray arraywithcontentsoffile:messagespath]; [messagesarray addobject:newmessagedictionary]; //this causes crash //write messagesarray file nsstring *plistpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) lastobject]; plistpath = [plistpath stringbyappendingpathcomponent:@"usersmessages.plist"]; [messagesarray writetofile:plistpath atomically:yes];
so understand trying add compiler see's immutable array declared mutable?
whats going on? :(
you re-initializing messagesarray
[nsarray arraywithcontentsoffile:messagespath]
making not mutable. try:
[nsmutablearray arraywithcontentsoffile:messagespath];
Comments
Post a Comment