我有一个从JSON API获取的资源。
JSON被解析为NSDictionary,在本例中称为game
。
我正在基于JSON的属性创建我的Game类的新实例。
游戏类有一个名为 userRegistered 的属性,其定义如下:
// 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);
字典中的“user_registered”键将始终为1或0。
Xcode警告我,我有 -
warning: Semantic Issue: Incompatible integer to pointer conversion passing 'int' to parameter of type 'BOOL *' (aka 'signed char *')
有人可以解释一下这个问题以及如何解决它吗?
我的完整游戏类定义如下:
#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
我在此方法中的一个ViewControllers中收到错误
// 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;
}
警告显示超过newGame.userRegistered = [[game objectForKey:@"user_registered"] intValue];
答案 0 :(得分:4)
[game objectForKey:@“user_registered”]可能会给你一个NSNumber对象。您可能的意思是比较NSNumber对象中的整数值。
([[game objectForKey:@"user_registered"] intValue] > 0)
更新以响应您的更新:
您的问题在于如何宣布您的BOOL属性 - 您有*需要删除。
@property (nonatomic, assign) BOOL *userRegistered;
应该是
@property (nonatomic, assign) BOOL userRegistered;
答案 1 :(得分:1)
我只需使用boolValue
即可解决此问题game.userRegistered = [[json objectForKey:@"user_registered"] boolValue];
感谢大家的帮助
答案 2 :(得分:0)
objectForKey
函数将返回一个objective-c实例。
([[game objectForKey:@"user_registered"] boolValue] > 0)
答案 3 :(得分:0)
([game boolForKey:@“user_registered”] == YES)