NSNumber math

Discussion in 'iOS Programming' started by xaphann, May 25, 2012.

  1. xaphann macrumors newbie

    Joined:
    Oct 22, 2011
    #1
    Apparently my mind is shot and ready for the week... having an issue doing some basic math. I need to add up a bunch of NSNumber into a total amount doing this;
    Code:
    NSNumber *total = number1 + number2 + number3;
    
    Is giving me an error "invalid operands to binary expression (nsnumber * and nsnumber *)"

    Just a note number1, number2 and number3 are defined as NSNumbers in core data entity.

    thanks
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Objective-C isn't Java. You can't do arithmetic on objects that represent numbers.

    Get the value from the NSNumber object (int, float, double, whatever), then do arithmetic, then rewrap it in an NSNumber if needed.
     
  3. xaphann thread starter macrumors newbie

    Joined:
    Oct 22, 2011
    #3
    oops never replied to this. That worked great thanks.
     

Share This Page