user3746809
user3746809

Reputation: 23

Invalid Operands to Binary Expression (Int and NSNumber *)

I'm learning objective C from CodeSchool and there's one section of learning OOP which I don't understand why it won't work but seems so simple to fix.

The Code:

- (void) decreaseBatteryLife:(NSNumber *)decreaseBy
{
    self.batteryLife = @([self.batteryLife intValue] - decreaseBy);
}

The error which points to the minus symbol before decreaseBy:

^invalid operands to binary expression ('int' and 'NSNumber *')

Upvotes: 2

Views: 1677

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

"decreaseBy" is a NSNumber object while the other value is a "int". There's a difference (the first is an Objective C object, the other is a raw C type).

You need to get the raw "intValue" of your "decreaseBy" number.

Something like this:

self.batteryLife = @([self.batteryLife intValue] - [decreaseBy intValue]);

Upvotes: 2

Related Questions