Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C Boolean Array

I need to utilize an array of booleans in objective-c. I've got it mostly set up, but the compiler throws a warning at the following statement:

[updated_users replaceObjectAtIndex:index withObject:YES]; 

This is, I'm sure, because YES is simply not an object; it's a primitive. Regardless, I need to do this, and would greatly appreciate advice on how to accomplish it.

Thanks.

like image 823
Allyn Avatar asked Mar 09 '09 22:03

Allyn


People also ask

Is there a Boolean array in C?

Boolean Arrays in C: Like normal arrays, we can also create the boolean arrays using the data type bool from stdbool. h header file in C. The boolean array can store multiple true or false values for each element and all elements and can be accessed by using indexes.

Can we make array of Boolean?

An array of booleans are initialized to false and arrays of reference types are initialized to null. In some cases, we need to initialize all values of the boolean array with true or false. We can use the Arrays. fill() method in such cases.


1 Answers

Yep, that's exactly what it is: the NS* containers can only store objective-C objects, not primitive types.

You should be able to accomplish what you want by wrapping it up in an NSNumber:

[updated_users replaceObjectAtIndex:index withObject:[NSNumber numberWithBool:YES]]

or by using @(YES) which wraps a BOOL in an NSNumber

[updated_users replaceObjectAtIndex:index withObject:@(YES)]]

You can then pull out the boolValue:

BOOL mine = [[updated_users objectAtIndex:index] boolValue];

like image 136
Nick Partridge Avatar answered Oct 08 '22 20:10

Nick Partridge