开发者

Expression Result Unused Error in Xcode

Why am I getting this error?

if(appDelegate.lastLevelIndex!=0) {
        [self.view addSubview:creditView];
        NSString *level=[NSString st开发者_C百科ringWithFormat:(@"%d",appDelegate.lastLevelIndex)];
        label.text=level;
        appDelegate.lastLevelIndex=0;
    }

I also get a crash here due to the original error.


Remove the parentheses here:

NSString *level=[NSString stringWithFormat:(@"%d",appDelegate.lastLevelIndex)];
                                           ^                                ^

Resulting in:

NSString *level=[NSString stringWithFormat:@"%d",appDelegate.lastLevelIndex];

The method stringWithFormat: expects a list of comma-separated arguments. The first argument is a format string that serves as a template into which the remaining argument values are substituted.

With the parentheses in place, however, you are passing a single argument to stringWithFormat:, the result of evaluating the expression contained within the parentheses.

(@"%d",appDelegate.lastLevelIndex)
      ^

In C and, by extension, Objective-C a list of expressions separated by a comma is evaluated left to right, and the type and value of the result are the type and value of the right operand.

Therefore, you are passing the value of appDelegate.lastLevelIndex, an integer, as the only argument to the stringWithFormat: method, which expects a NSString *. Hence, the crash. The "Expression Result Unused" warning is due to the fact that the left expression @"%d" has no effect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜