Is there a problem using int property for a class?

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

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

So which is better:

@interface xx... 
    int myProp;
@end

      

or

@interface xx... 
    NSInteger *myProp;
@end

      

0


a source to share


2 answers


The int version is fine, but NSInteger is preferred. NSInteger is not an object and should not refer to a pointer - it is just a typedef that will allow a variable to be a native word size on 32-bit and 64-bit computers. So the best option is:



@interface SomeClass : NSObject {
    NSInteger aNumber;
@end

@implementation SomeClass
- (id)init {
    [super init];
    number = 42;
}
@end

      

+2


a source


See question . NSInteger is architecture safe and has been recommended since 10.5 years.



0


a source







All Articles