【问题标题】:What does this warning mean in Xcode?这个警告在 Xcode 中是什么意思?
【发布时间】:2012-02-16 18:59:06
【问题描述】:

我有一个从 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

我在这个方法中的一个 ViewController 中遇到了错误

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

【问题讨论】:

  • objectForKey 返回一个Objective-C实例,所以肯定有问题;但是,这似乎不是警告的内容。你确定你显示的是正确的线吗?
  • object For user_registered 是布尔值还是整数?

标签: objective-c xcode4.2


【解决方案1】:

[game objectForKey:@"user_registered"] 可能会给你一个 NSNumber 对象。您可能的意思是比较该 NSNumber 对象内的整数值。

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

更新以响应您的更新:

您的问题在于您声明 BOOL 属性的方式 - 您有一个需要删除的 *。

@property (nonatomic, assign) BOOL *userRegistered;

应该是

@property (nonatomic, assign) BOOL userRegistered;

【讨论】:

  • 谢谢,但即使您的更新,它仍然给出同样的错误:/
  • 我认为是时候拿出好的'ol 调试器并单步调试代码了。将[game objectForKey:@"user_registered"] 放入一个var中,读取它是什么类型的对象。
  • @jurgemaister - 它作为__NSCFNumber 的实例出现。有什么线索吗?
  • 没关系。您可以像对待 NSNumber 一样对待 __NSCFNumber 对象。它们是所谓的“类集群”的一部分(在 Xcode 的文档中搜索“类集群”以获得解释)。
【解决方案2】:

我可以通过简单地使用 boolValue 来解决这个问题

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

感谢大家的帮助

【讨论】:

    【解决方案3】:

    objectForKey 函数将返回一个objective-c 实例。

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 游戏是 NSDictionary 类型的,它没有 boolForKey: 方法。它只有 objectForKey:.
      • 对不起。我以为是 NSUserDefaults
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-17
      • 2011-09-25
      • 2012-02-20
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多