2

I have my UITableView data organized such that the sections of the table are the array elements of a NSMutableArray. Each element itself is a NSMutablearray of NSMutableDictionary, representing the row of each section and the data for each row.

When I create this, I load all the data and create each section's NSMutableArray, then add them one at a time to the "outer" NSMutableArray. I reference the rows by:

[[[objData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"]

and have had no problems doing this. I think the advantage here is I can have a variable number of sections, which I need, and a variable number of rows in each section, also a requirement.

I do, however, get an error when I try to add a row to a section.

[[objData objectAtIndex:0] addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys: objectID,@"id",nameVar,@"name",nil]];

doesn't work. Is this the problem, or is there a problem with the way I've layed out the array? I'm thinking that the inner NSMutableArray is only alloted so much memory, but I would have thought the array is just an array of references and wouldn't have that issue. I could be wrong - it isn't the first time.

Thanks in advance for wrapping your head around this.

4
  • What error do you get? I'm thinking that the outer array is actually an NSArray, not an NSMutableArray, so you can't add an object to it. But no, there's no memory issue; you're right about the array of references. Commented Nov 24, 2011 at 18:37
  • Hi matt, I just get "Program received signal:"SIGABRT". The console msg:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object' You may be onto something in regards to the NSArray, but I'm looking at everything, and it all seems mutable. Commented Nov 24, 2011 at 18:48
  • Yep, I was right - you've got an immutable NSArray and you're trying to add an objec to it. You can't do that. - Remember, you can call your object anything you like, but objects have their own innate type - what matters is not how you type or describe it but what it really is, internally. Commented Nov 24, 2011 at 19:52
  • Hi Matt, as you can see below, you are right. It happened from me extracting it from NSUSerDefaults. Commented Nov 24, 2011 at 22:54

1 Answer 1

1

How are you intializing the array and the values inside it? I think you have an array that is not mutable.

Also an NSMutableArray has a variable size, calling initWithSize is completely unnecessary. The array will resize if it needs to.

Sign up to request clarification or add additional context in comments.

5 Comments

In this view, I load objData from NSDefaults using self.objData = [defaults mutableArrayValueForKey:kObjDataDefaultKey]; It's created and saved in another view. I'm not sure what you mean in regards to the initWithSize comment.
Upon further searching along these lines - it appears things coming out of NSDefaults become immutable. I will have to look for alternatives or workarounds. Thanks!
use [[NSMutableARray alloc] initWithArray for that.
I think this is still within the scope of this question - I will consider either of your suggestions, but I have a nested set of arrays - on first attempt, it doesn't seem to work. Is there a simple solution without having to loop through and copy everything to the new NSMutableArray?
You don't need to loop or copy anything to turn an NSArray into an NSMutableArray. These are just pointers - you said so yourself. Simply call [NSMutableArray arrayWithArray:originalArray] and kaboom, you've got your mutable array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.