Custom Bonbons
Custom Bonbons

Reputation: 1699

Updating Bool value in NSMutableArray

I have added a Bool value to a NSMutableArray like this:

[myMutableArray insertObject:[NSMutableArray arrayWithObjects:
                                       [NSNumber numberWithBool:YES],
                                       …,
                                       …,
                                       nil] atIndex:i];

but how do I later update the value to NO! Seems like a ridiculously simple question, but I am getting errors such as 'Expression is not assignable'.

This, for example, doesn't work:

[[myMutableArray objectAtIndex:i] objectAtIndex:0] = NO;

Advice appreciated. Thanks.

Upvotes: 0

Views: 1242

Answers (2)

Brayden
Brayden

Reputation: 1795

Take the snippet below for a test run:

NSNumber *newValue = [NSNumber numberWithBool:NO]; //toggle the value of the BOOL value

[mutableArray replaceObjectAtIndex:[[mutableArray objectAtIndex:i] objectAtIndex:0] withObject:newValue];

Unless you're able to use literals, then use rmaddy's answer.

Upvotes: 3

rmaddy
rmaddy

Reputation: 318814

Use the replaceObjectAtIndex:withObject: method.

If you are using the latest Xcode and compiler then it's even easier:

myMutableArray[someIndex] = @NO;

Upvotes: 2

Related Questions