开发者

Warning when I fill an array with BOOL

I have this code:

BOOL booleanValue = TRUE;
[arrayZone replaceObjectAtIndex:indexZone withObject:booleanValue];

This code gives me a warning that says:

incompatible integer to poi开发者_如何学运维nter conversion: 
sending BOOL to parameter of type 'id'

Why?


You need to box your BOOL with a NSNUmber like this:

BOOL booleanValue = TRUE;
[arrayZone replaceObjectAtIndex:indexZone withObject:[NSNumber numberWithBool:booleanValue]];

Then, to retrieve your BOOL value, you unbox it using boolValue:

BOOL b = [[arrayZone objectAtIndex:index] boolValue];


You can only store objects within an NSArray, not basic types.

Cheers


This question seems to be a duplicate of Objective C Boolean Array

In essence, BOOL is a primitive type - you have to wrap it in an Object to get rid of the warning.


BOOL is a primitive type and your array requires an object. That's why you need to wrap it up in a NSNumber. But with newer xcode you can just type @YES or @NO and xcode will treat it like a numberWithBool. So, instead of:

    BOOL booleanValue = TRUE;
    [arrayZone replaceObjectAtIndex:indexZone withObject:[NSNumber numberWithBool:booleanValue]];

you can now use

    [arrayZone replaceObjectAtIndex:indexZone withObject:@YES];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜