Snowman
Snowman

Reputation: 32071

Expression Result Unused Error in Xcode

Why am I getting this error?

if(appDelegate.lastLevelIndex!=0) {
        [self.view addSubview:creditView];
        NSString *level=[NSString stringWithFormat:(@"%d",appDelegate.lastLevelIndex)];
        label.text=level;
        appDelegate.lastLevelIndex=0;
    }

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

Upvotes: 0

Views: 3660

Answers (1)

albertamg
albertamg

Reputation: 28572

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.

Upvotes: 4

Related Questions