1

I have a resource which is fetched from a JSON API. The JSON is parsed into a NSDictionary which, in this case is called game.

I'm creating a new instance of my Game class based on the attributes from the JSON.

Game class has a property called userRegistered which is defined as follows:

// in Game.h
@interface
@property (nonatomic, assign) BOOL userRegistered;


// elsewhere in my code I have
Game *newGame = [[Game alloc] init];
newGame.userRegistered = ([game objectForKey:@"user_registered"] > 0);

The "user_registered" key in the dictionary will always be either 1 or 0.

Xcode warns me the I have - warning: Semantic Issue: Incompatible integer to pointer conversion passing 'int' to parameter of type 'BOOL *' (aka 'signed char *')

Can someone please explain the issue and how I might resolve it?

Update

My full game class is defined as follows:

#import <Foundation/Foundation.h>

@interface Game : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *photoURL;
@property (nonatomic, copy) NSString *gameURL;
@property (nonatomic, assign) BOOL *userRegistered;

@end

// Game.m
#import "Game.h"

@implementation Game

@synthesize name = _name;
@synthesize partnerName = _partnerName;
@synthesize photoURL = _photoURL;
@synthesize gameURL = _gameURL;
@synthesize userRegistered = _userRegistered;

@end

I'm getting the error in one of my ViewControllers in this method

     // api_response.body has just been set to an __NSCFArray containing 
     // NSDictionaries by AFNetworking
     NSDictionary *game;
     Game *newGame;
     for (game in api_response.body){
         newGame = [[Game alloc] init];

         NSLog(@"Creating a new game");
         // set attributes for new game instance
         newGame.name = [game objectForKey:@"name"];
         newGame.photoURL = [game objectForKey:@"photoURL"];
         // user registered is either 0 (false) or 1 (true)
         newGame.userRegistered = [[game objectForKey:@"user_registered"] intValue];

         // add the game instance to the appropriate array
         [self addGameToGamesArray:newGame];
         newGame = nil;
     }

The warning shows over newGame.userRegistered = [[game objectForKey:@"user_registered"] intValue];

bodacious
  • 6,608
  • 9
  • 45
  • 74
  • `objectForKey` returns an Objective-C instance, so sure is a problem; however, that doesn't seem to be what the warning is about. Are you sure you're showing the right line? – zneak Feb 16 '12 at 19:16
  • object For user_registered is boolean or integer ? – Parag Bafna Feb 16 '12 at 19:59

4 Answers4

4

[game objectForKey:@"user_registered"] is likely giving you an NSNumber object. You probably mean instead to compare the integer value inside that NSNumber object.

([[game objectForKey:@"user_registered"] intValue] > 0)

UPDATE in response to your update:

Your problem is with how you're declaring your BOOL property - you have a * that you need to remove.

@property (nonatomic, assign) BOOL *userRegistered;

should be

@property (nonatomic, assign) BOOL userRegistered;
commanda
  • 4,841
  • 1
  • 25
  • 34
  • Thanks but even with your update, it's still giving the same error :/ – bodacious Feb 16 '12 at 19:25
  • 1
    I think it's time you bring out the good 'ol debugger and step through your code. Put the `[game objectForKey:@"user_registered"]` into a var, and read what kind of object it is. – Jørgen R Feb 16 '12 at 20:38
  • @jurgemaister - It's coming out as an instance of `__NSCFNumber`. Any clues? – bodacious Feb 17 '12 at 18:21
  • [I think this is what you are looking for](http://stackoverflow.com/questions/4357063/what-is-the-class-nscfnumber-in-ios-4-1) – Jørgen R Feb 17 '12 at 18:46
  • That's fine. You can treat __NSCFNumber objects the same as you treat NSNumbers. They're part of what's called a "Class Cluster" (search for "Class Clusters" in Xcode's Documentation for an explanation). – commanda Feb 17 '12 at 19:02
1

I was able to solve this issue by simply using boolValue

game.userRegistered = [[json objectForKey:@"user_registered"] boolValue];

Thanks all for the help

bodacious
  • 6,608
  • 9
  • 45
  • 74
0

objectForKey function will return an objective-c instance.

([[game objectForKey:@"user_registered"] boolValue] > 0)
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
0

([game boolForKey:@"user_registered"]==YES)

Vladimir Stazhilov
  • 1,956
  • 4
  • 31
  • 63