0

I have two NSMutableArray: arrayone and arraytwo

arrayone and arraytwo are full of objects, but I want cancel all objects of arrayone and insert in this array all objects of arraytwo. then I want overwrite arrayone with arraytwo.

9
  • 2
    A quick look at NSArray documentation... Commented May 19, 2011 at 14:02
  • So you want to swap contents of both arrays? Or just send all of arrayone's contents to arraytwo, and release arrayone? Sorry, I'm just a little confused about your intentions. Commented May 19, 2011 at 14:02
  • I want replace all objects of arrayone with objects of arraytwo Commented May 19, 2011 at 14:05
  • 4
    Use the removeAllObjects: method on arrayone, then [arrayone addObjectsFromArray:arraytwo]. Commented May 19, 2011 at 14:08
  • 1
    @Avizz92:It is better, if you post it as answer. Commented May 19, 2011 at 14:09

2 Answers 2

3

Lets say you have:

NSMutableArray *arrayOne = [[NSMutableArray alloc]initWithObjects:@"A", @"B",nil ];
NSMutableArray *arrayTwo = [[NSMutableArray alloc]initWithObjects:@"C", @"D",nil ];

Now you can remove all objects and insert the new objects like this:

[arrayOne removeAllObjects];
[arrayOne addObjectsFromArray:arrayTwo];

Or release the array and initialize a new one like this:

[arrayOne release];
arrayOne = [[NSMutableArray alloc] initWithArray:arrayTwo];

In both cases NSLog(@"%@",arrayOne); shows:

(
    C,
    D
)
Sign up to request clarification or add additional context in comments.

Comments

0

arrayOne = [[NSMutableArray alloc] initWithArray:arrayTwo copyitems:YES];

Comments

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.