计算方法上的给定数量会产生此错误

时间:2013-09-16 10:34:42

标签: ios objective-c

我有这个方法:

- (void) voteResult: (NSString *)partyName votedNumber:(int)votedNumber
{
    int *moreVotes;
    moreVotes = 2000 - votedNumber;

    _labelVoteResult.text = @"Currently, your party (%x) has %i votes. We need %i more votes to put %x on our list. Share to your Facebook or Twitter to get more votes from your downline or upline.", partyName, votedNumber, moreVotes, partyName;
}

但我收到此错误消息:

Incompatible integer to pointer conversion assigning to 'int *' from 'int'

因为这一行:

moreVotes = 2000 - votedNumber;

我也有这个错误信息:

Expression result unused

因为这一行(partyName):

_labelVoteResult.text = @"Currently, your party (%x) has %i votes. We need %i more votes to put %x on our list. Share to your Facebook or Twitter to get more votes from your downline or upline.", partyName, votedNumber, moreVotes, partyName;

最后一点,%x应该是一个字符串,但Objective C只有%i表示整数,%c表示char。我不知道字符串究竟是什么。

谢谢。

1 个答案:

答案 0 :(得分:3)

该方法应如下所示:

- (void) voteResult: (NSString *)partyName votedNumber:(int)votedNumber
{
    // Not int*
    int moreVotes = 2000 - votedNumber;

    // Use [NSString stringWithFormat:] with partyName using the %@ format specifier
    _labelVoteResult.text = [NSString stringWithFormat:@"Currently, your party (%@) has %i votes. We need %i more votes to put %@ on our list. Share to your Facebook or Twitter to get more votes from your downline or upline.", partyName, votedNumber, moreVotes, partyName);

}