If I can write:
test_string = [[NSString alloc] init]; and
test_date = [[NSDate alloc] init];
Why, if only for reasons of consistency, can’t I write:
test_integer = [[NSInteger alloc] init]; or
test_decimal = [[NSDecimal alloc] init];
and how do I set them up?
Your first two examples are real objects, so they must be instantiated with
alloc/init(or the shorthand form,new.An
NSIntegeris just a typedef tointorlong, depending on the platform, that is, it is just another name for eitherintorlong. Those are both so-called primitive types. They’re not real objects, but something much simpler. They don’t have any methods, iVars, etc. – in fact, they really are just numbers, while an object representing a number (for example an[NSNumber numberWithInt:1]) is much more.Primitive types are used when speed and memory efficiency are important, and integers are used so often that those considerations are important here. By the way, Smalltalk (the language that inspired Objective-C’s syntax and object model) did not have primitive types. Even integers, even boolean values were objects. This was much more consistent (and more powerful in some regards), but it was one of the factors that made Smalltalk quite slow in the seventies and early eighties.
NSDecimalis also not an object, but a struct. A struct is essentially a list of named primitive values. Other often-used structs in Objective-C where this might be more obvious areCGPoint(A struct containing two floats, x and y),CGSize(again a struct containing two floats, this time called width and height),CGRect(a struct containing aCGPointstruct named origin, and aCGSizestruct named size).