4

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241

4 Answers4

14

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];
BryanH
  • 5,826
  • 3
  • 34
  • 47
Nyx0uf
  • 4,609
  • 1
  • 25
  • 26
0

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];
Objectif
  • 364
  • 1
  • 4
  • 12
0

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

Cheers

Fran Sevillano
  • 8,103
  • 4
  • 31
  • 45
0

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.

Community
  • 1
  • 1
Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107