views:

112

answers:

2

When I started with Cocoa, I remember that I read somewhere that int/float and similar should not be used for class properties and to use NS* equivalents (like NSInteger).

Is there a real hidden issue here why would that be better or it was just a voluntary coding rule by a person where I read that (and I can't for the life of me find where was that)?

So, what is better:

@interface xx... 
    int myProp;
@end

or

@interface xx... 
    NSInteger *myProp;
@end
+2  A: 

The int version is fine, but NSInteger is preferred. NSInteger isn't an object and doesn't have to be referenced with a pointer — it's just a typedef that will allow the variable to be the native word size on both 32-bit and 64-bit computers. So the best option would be:

@interface SomeClass : NSObject {
    NSInteger aNumber;
@end

@implementation SomeClass
- (id)init {
    [super init];
    number = 42;
}
@end
Chuck
So it's basically a safe guard against pushing through the int size? Then there's nothing wrong to using int, if I'm certain it will be small maximum number (like 1000).
Aleksandar Vacic
It's not a safeguard against anything. It just helps make your app architecture-independent. There is generally no good reason to use int over NSInteger — you don't gain anything. If you want to specify a small number and therefore conserve space, you should use a small type such as uint_16 or uint_8.
Chuck
Thanks Chuck, much appreciated.
Aleksandar Vacic
A: 

See this question. NSInteger is architecture safe and is recommended from 10.5 onwards.

pgb