views:

5096

answers:

4

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.

+2  A: 

You can either store NSNumbers:

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

or use a C-array, depending on your needs:

BOOL array[100];
array[31] = YES;
Georg
+4  A: 

Assuming your array contains valid objects (and is not a c-style array):

#define kNSTrue         ((id) kCFBooleanTrue)
#define kNSFalse        ((id) kCFBooleanFalse)
#define NSBool(x)       ((x) ? kNSTrue : kNSFalse)

[updated_users replaceObjectAtIndex:index withObject:NSBool(YES)];
Andrew Grant
+13  A: 

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]]

You can then pull out the boolValue:

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

Nick Partridge
A: 

How can you use that C-array as a property of a class?