1

For example: I have a mutable array of BOOLs(or [NSNumber numberWithBool:TRUE]; ). and then I want to change, for example the first value in this array to FALSE.

[array addObject:[NSNumber numberWithBool:TRUE]];
[array objectAtIndex:0] ??? 

I know a quite strange way to do this task.

[array replaceObjectAtIndex:0 withObject:[NSNumber numberWithBool:FALSE]];

Yes, this fully satisfies me, but I am looking for another way, that is more simple than this. Something like [[array objectAtIndex:0] setBoolValue:FALSE]; for example, imagine, if I had UIButton instead of BOOLs: [[array objectAtIndex:0] setHidden:TRUE]; or this is only dreams?

Thanks in advance!

3 Answers 3

10

The best way to do this is

[array replaceObjectAtIndex:0 withObject:[NSNumber numberWithBool:FALSE]];

The reason being that [array objectAtIndex:0] returns an NSNumber, unlike if you had an array of UIButtons which would return a UIButton object that you could operate on. Since you're getting an NSNumber in return, you can't do anything to do the NSNumber directly since it doesn't have the methods you want, so you instead have to operate on the array that holds the NSNumber.

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

2 Comments

Oh, now that is amlost clear for me. It seemes that NSNumber just does not have such method.
I tried using replaceObjectAtIndex on an NSMutableArray of NSMutableStrings, and I'm getting an "unrecognized selector sent to instance" error. Is there anything special you have to do when it's an array of strings? I also realize that this is a pretty old thread, is it possible something has changed since this was written (I am using ARC, so could that be releasing something it shouldn't)?
2

There is nothing stopping you from sending mutating messages to an object in an array, but NSNumber is simply not mutable. That's why you have to replace the number object with a different one. It's nothing to do with the array or the language — you just can't change an NSNumber.

4 Comments

Okay, but if I use BOOL instead of NSNumber? and the problems remains as I tried.
@URLArenzo: It is not valid to put a BOOL in an NSArray at all. NSArray can only hold objects, not scalars like BOOL or int.
oh, That's why I get warnings when I try to put a BOOL in a NSArray
Adding to Chuck's answer: 'immutable' is the English word that means the data inside the object cannot be changed. The NSNumber class does not mention 'immutable', but its superclass NSValue does. Confusion can come from the fact that you can have a mutable array, NSMutableArray, that contains immutable NSNumber objects. That means you can add or remove element positions (slots) in the array, but you cannot change the numeric value inside any of the NSNumber objects stored in those slots.
0

Something to understand here: Arrays are simple collections of pointers to object. Hence, you can easily to things like
((UIButton*)[array objectAtIndex:0]).hidden=TRUE;
Modifying the object is possible and often done. But exchanging one object for another one, you need to use Replacement techniques as you pointed out. However, simpler objects are, as said above by @Chuck, simply not mutable (like NSNumber).

Please also note that my code above is horrible in terms of stability. How do you certainly know that it's a UIButton you're pulling out of your magic hat (ie. the array?)

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.