In objective-c I am trying to evaluate the following expression: _c = _f / 5 * 8;
It tells me that an int and a NSNumber are invalid arguments to a binary expression.
Whats wrong? I am just getting started with objective-c and cocoa but am familiar with php and basic and kinda familiar with javascript.
Objective-C has several different structures in place for you to use in calculations. From its C roots come the primitive numbers,
ints,floats,doubles, etc, on which you can perform arithmetic operations directly (+,-,*,/, etc.). These are what you’re looking to use.On a higher lever, Objective-C also has
NSNumberobjects, which are simple wrappers for the primitive types listed above. They’re used throughout Objective-C where primitives need to be stored (often within other objects such as arrays and dictionaries that don’t take primitive values directly). BecauseNSNumbers are objects, you cannot perform direct arithmetic operations on them, you have to draw out their primitive values first (usingintValue, for instance, to get an integer value, ordoubleValueto get a double-precision floating point number). Because it’s unclear what the variables represent in your question, I’m not going to venture a guess as to what it is you’re trying to do (I don’t want to mislead you), but you can find out more aboutNSNumberin theNSNumberClass Reference.Finally, as Richard mentioned, there are
NSDecimalNumbers. These are almost never used, since they’re either simply not needed (they’re designed to hold extremely high-precision numbers, far beyond the capacity of regular primitive values), or too complicated to use. They also have their own methods for performing arithmetic operations, and are generally irrelevant for everyday use. Again, if you’re interested, look more into theNSDecimalNumberClass Reference.For the most part, you’re looking to use primitive numbers to do your calculations. When you need to store them, you can often ‘box’ and ‘unbox’ (store and retrieve) from
NSNumberobjects.