0

I have a first NSArray firstArray and I do

[firstArray removeAllObjects];

after I want fill it with the content of an other array secondArray

is it right write in this way?

firstArray = secondArray; 

2 Answers 2

1

No, firstArray = secondArray will reassign the pointers, this is not the behavior you want. You want [firstArray addObjectsFromArray:secondArray]

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

3 Comments

and if I use initwitharray? what happens?
You'd be breaking the contract of the object. Objects must only be initialized once, doing it multiple times WILL cause you problems down the road.
@JoshuaWeinberg What about firstArray = [secondArray copy];?
1

Since the firstArray is an NSArray, you will want to do this instead:

firstArray = [NSArray arrayWithArray:secondArray];

2 Comments

and if it's a nsMutableArray?
If firstArray is an NSMutableArray, you can use the addObjectsFromArray method that Joshua suggested.

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.