Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instance variables in @interface; header vs implementation

Is there any difference between declaring a private instance variable in the header vs declaring it in the implementation?

in TestObj.h

@interface TestObj : NSObject
{
    int test;
}
@end

vs in TestObj.m

@interface TestObj()
{
    int test;
}
@end

Both seem equivalent to me, is there any actual difference between declaring an instance variable in the header vs in the implementation, if not which is preferred? The @interface within the implementation file just seems like a way to declare private properties, does it have any other purpose outside that?

like image 514
Kevin DiTraglia Avatar asked Sep 04 '13 14:09

Kevin DiTraglia


1 Answers

The preference is generally to place private instance variables and properties in the private class extension (in the .m) and leave the public interface file (.h) for those properties and methods that are truly part of the public interface.

It helps isolate implementation details from the public interface and makes everything much cleaner. It also ensures that external classes do not inadvertently alter the private variables of this class.

See Class Extensions Extend the Internal Implementation.

like image 89
Rob Avatar answered Oct 10 '22 10:10

Rob