【问题标题】:setting method with int [closed]用int设置方法[关闭]
【发布时间】:2012-06-05 19:20:08
【问题描述】:

我有一个方法,当我想设置一个参数(类型是int)时,我收到一条错误消息。 我的代码:

@implementation PlayerInfo

@synthesize playerName;
@synthesize playerScore;

-(id) initPlayerInfo:(NSString*) name playerScore:(int) score
{
    self = [super init];

    if(self)
    {
        self.playerName = name;
        self.playerScore = score;
    }

    return self;
}

我在这一行得到一个错误:

self.playerScore = score;

有什么问题??

谢谢!!

【问题讨论】:

  • 首先,错误信息是什么?
  • playerScore 是如何声明的?
  • playScore 声明如下:@property (nonatomic, assign) int *playerScore;

标签: objective-c xcode methods int


【解决方案1】:

您将属性声明为指向 int(即 int *)而不是 int(即 int)的指针。尽管两者都包含关键字int,但它们是非常不同的东西。

【讨论】:

    【解决方案2】:

    playerScore 必须是(非原子的,分配的)检查。或者当你初始化 PlayerInfo 实例时,你应该给 score 一个值

    【讨论】:

    • 无法从给出的信息中看出这一点。你假设它是一个 int,它可能是,但在 OP 澄清之前,这个答案可能是错误的。
    猜你喜欢
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 2021-03-03
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多