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.
From XCode 4.4 you can use Objective-C literals.
[updated_users replaceObjectAtIndex:index withObject:@YES];
Where
@YES
is equivalent of[NSNumber numberWithBool:YES]
Like Georg said, use a C-array.
Martijn, "myArray" is the name you use, "array" in georg's example.
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 aBOOL
in anNSNumber
[updated_users replaceObjectAtIndex:index withObject:@(YES)]]
You can then pull out the boolValue:
BOOL mine = [[updated_users objectAtIndex:index] boolValue];
Assuming your array contains valid objects (and is not a c-style array):
If your collection is large or you want it to be faster than objc objects, try the
CFBitVector
/CFMutableBitVector
types found in CoreFoundation. It's one of the CF-Collections types which does not ship with a NS counterpart, but it can be wrapped in an objc class quickly, if desired.You can either store
NSNumbers
:or use a C-array, depending on your needs: