cyclingIsBetter
cyclingIsBetter

Reputation: 17591

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 pointer conversion: 
sending BOOL to parameter of type 'id'

Why?

Upvotes: 4

Views: 3982

Answers (4)

Objectif
Objectif

Reputation: 374

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

Upvotes: 0

Nyx0uf
Nyx0uf

Reputation: 4649

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

Upvotes: 14

Frank Schmitt
Frank Schmitt

Reputation: 30765

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.

Upvotes: 0

Fran Sevillano
Fran Sevillano

Reputation: 8163

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

Cheers

Upvotes: 0

Related Questions