Palindrome
Palindrome

Reputation: 161

Retrieve ValueForKey BOOL value

I mark some objects as inactive

[apIndicators[i] setValue:[NSNumber numberWithBool:NO] forKey:@"isActive"];

Later I set some of them as active. Then I have to check all inactive objects left in 'NSMutableArray'.

I have to do that in for loop

for (int i = 0; i < apCount; i++) {
     if [apIndicators[i] valueForKey:@"isActive"]) { //the problem is here
          //do some stuff with object
     }
}

How can I get YES or NO value of every object in array? Thanks in advance.

Upvotes: 4

Views: 3449

Answers (1)

T. Benjamin Larsen
T. Benjamin Larsen

Reputation: 6393

BOOL active = [[apIndicators[i] valueForKey:@"isActive"] boolValue];

if (active){
    // do whatever
}

If those apIndicators are all NSDictionaries you could use the fast enumerator to simplify the loop quite a bit as well.

for (NSDictionary *apIndicator in yourArray){
    BOOL active = [[apIndicator valueForKey:@"isActive"] boolValue];
    if (active){
        // do whatever
    }
}

Upvotes: 13

Related Questions