0

I'm trying to rewrite one element from self.tableData to another.

my NSMutableArray:

self.tableData = [[NSMutableArray alloc] initWithObjects:
                          [[Cell alloc] initWithName:@"dawdw" andImage:@"dwddw" andDescription:@"dawdw" andTypes:@"dawwd dawwd" andforWho:@"dwaadw"],
                          [[Cell alloc] initWithName:@"Kabanos" andImage:@"spodwwdwdrt.jpg" andDescription:@"dwdw" andTypes:@"dwdw dww" andforWho:@"dawwd"],
                          [[Cell alloc] initWithName:@"dwwd" andImage:@"dwwd" andDescription:@"dwwd" andTypes:@"wdwd daww" andforWho:@"dadawwa"],nil];

NSMutableArray *newarray;
[newarray addObject:self.tableData[0]];

But it's not working, maybe it's a newbie question but i have never before worked with arrays with many objects inside.

With self.tableData[0] i men rewrite object

[[Cell alloc] initWithName:@"dawdw" andImage:@"dwddw" andDescription:@"dawdw" andTypes:@"dawwd dawwd" andforWho:@"dwaadw"],
1
  • "It's not working" is not an adequate problem description. Commented Jul 11, 2014 at 16:33

3 Answers 3

1
NSMutableArray *newarray;
[newarray addObject:self.tableData[0]];

The problem with the code above is that you haven't created an array for newArray to point to, so the value of newArray is nil. Do this instead:

NSMutableArray *newarray = [NSMutableArray array];
[newarray addObject:self.tableData[0]];

Now newArray will point to a valid mutable array to which you can add objects.

Also, realize that even with the fixed code, newArray[0] will point to the very same object that you've stored in self.tableData[0], not a copy. If you want it to point to a different object that contains similar data, you should either make a copy of the object or instantiate a new one, e.g.:

[newarray addObject:[self.tableData[0] copy]];

or:

[newarray addObject:[[Cell alloc] initWithName:@"dawdw" andImage:@"dwddw" andDescription:@"dawdw" andTypes:@"dawwd dawwd" andforWho:@"dwaadw"]];
Sign up to request clarification or add additional context in comments.

Comments

0

You need to init new array. You can do it like so:

NSMutableArray *newarray = [NSMutableArray array];

Comments

0

The problem here is you didn't initialize newarray. You need to do

newarray = [NSMutableArray array];

But also note that adding self.tableData[0] to it is different from adding another alloc'ed and init'ed Cell to it because the former increases the reference count on self.tableData[0], while the latter creates a new Cell object.

This also means that if you make a new Cell object and add it to the mutable array, and later on modified that object in the mutable array, it wouldn't change the cell in the first array. But if you did it the first way, it would.

Comments

Your Answer

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