ios - Updating a value in an NSMutable Array -
ios - Updating a value in an NSMutable Array -
i trying update specific key in nsmutablearray
. array called listfortable , trying update key statusreport. there 4 objects in array. trying update first.the next causing error:
[listfortable replaceobjectatindex:[[listfortable objectatindex:0] objectforkey:@"statusreport"] withobject:@"no edits"];
the array created in next way:
[listfortable addobject:[nsdictionary dictionarywithobjectsandkeys: itemname, @"itemname", @"", @"statusreport", nil]];
can explain why?
the error message is:
*** terminating app due uncaught exception 'nsrangeexception', reason: '*** -[__nsarraym replaceobjectatindex:withobject:]: index 1087320 beyond bounds [0 .. 3]' *** first throw phone call stack: (0x2c57df87 0x39f1ac77 0x2c49b331 0xbe9bb 0x2f9fc86d 0x2f9fc5dd 0x300511f7 0x2fcc5b51 0x2fcdd933 0x2fcdf8cb 0x2fadc615 0xab051 0x2fa2d497 0x2fa2d439 0x2fa1804d 0x2fa2ce69 0x2fa2cb43 0x2fa26451 0x2f9fccc5 0x2fc70513 0x2f9fb707 0x2c544807 0x2c543c1b 0x2c542299 0x2c48fdb1 0x2c48fbc3 0x337c4051 0x2fa5ba31 0x604d5 0x3a4b6aaf) libc++abi.dylib: terminating uncaught exception of type nsexception
this error:
[listfortable replaceobjectatindex:[[listfortable objectatindex:0] objectforkey:@"statusreport"] withobject:@"no edits"];
you getting parameters mixed up.
[listfortable replaceobjectatindex:...]
should taking integer
but
[[listfortable objectatindex:0] objectforkey:@"statusreport"]
is not integer.
rather replace it, pointer it:
nsmutabledictionary *entry = [[listfortable objectatindex:0] mutablecopy];
and update it:
entry[@"statusreport"] = @"no edits";
and replace:
[listfortable replaceobjectatindex:0 withobject:[entry copy]];
logic being: can't modify nsdictionary (it immutable) need mutable re-create can change. 1 time alter it, need replace it.
the
[entry copy]
bit makes immutable dictionary again.
truthfully, doesn't want array contain immutable objects anyway, hope explains issues
ios objective-c nsmutablearray
Comments
Post a Comment