Search code examples
iosnsstringexpressionunused-variables

'Expression myExpression unused' in NSString when using variables


I have this method where I try to set the result in "_result". If I use a formatted string to assigned it to "_result" it gives me the error 'Expression result unused'. The error does not show up if I use a plain string as @" done", and NSlog prints correctly the string gives me problem. Any suggestion how I can fix this? Thank you!

@property (nonatomic) NSString * result;

@synthesize result = _result;

- (NSString *) _result
{
    if (!_result) {
        _result= [[NSString alloc] initWithFormat:@"Could not compare %e"];
    }
    return _result;
}

- (NSString *) comparePrice:(double) price_one to_price2:(double) price_two;
{
    if (price_one > price_two) {
        NSLog(@"LOG result is %e ",price_one);
        _result = @"result is %e ",price_one;
        _result = @" done";
    }
    return _result;
}

Solution

  • Your error is on this line:

    _result = @"result is %e ",price_one;
    

    The compiler doesn't know what to do with the "price_one" bit. You need to instruct the compiler that you're attempting to use a format string.

    Change that line of code to:

    _result = [NSString stringWithFormat: @"result is %e ",price_one]; 
    

    and you should be good.