简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM