Lucabro
Lucabro

Reputation: 525

reinitialize a NSArray

If I've to reinitialize a NSArray with others values, is it right to do this?

NSArray *array = [[NSArray alloc] initWithObjects:obj1, obj2, nil];
// ...
// some code
// ...
array = [[NSArray alloc] initWithObjects:obj3, obj4, nil];

thanks

Upvotes: 0

Views: 314

Answers (4)

ppalancica
ppalancica

Reputation: 4277

I thinks this code may help you. At least, I thing this may be a suitable solution, especially if you are using ARC:

NSObject *obj1 = [NSNull null]; NSObject *obj2 = [NSNull null];

NSMutableArray *arrayObj = [NSMutableArray arrayWithObjects:obj1, obj2, nil];

[arrayObj removeAllObjects];

arrayObj = [NSMutableArray arrayWithObjects:obj1, obj2, nil];

I hope it helps you :)

Upvotes: 1

Alex
Alex

Reputation: 403

you can reinitialize NSArray with same name ,you will not get any error or warning but the latest objects gets replaced with previous objects.The previous objects overwrites. For this you have to use ARC otherwise memory problem will occurs.

Upvotes: 0

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81868

Your code does not re-initialze an NSArray. It just assigns a new object to the variable array. That's fine.

Upvotes: 2

gsach
gsach

Reputation: 5775

Yes this is absolutely right. The new object is completely different than the previous. The object pointer now points to a new object and the old one will be released, since you are using ARC.

It is not exactly the same as reinitializing because you throw away the object and getting a new, but NSArray is immutable so this is the only way to "reinitialize" it.

Upvotes: 3

Related Questions