Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacMan988

macrumors 6502a
Original poster
Jul 7, 2012
833
116
Hi, I have seen instance variable being declared in various places such as in the header file, in the implementation file etc.

u6FAVE.png


3XnpWg.png


XFHL2b.png


What would be the best practice for declaring instance variables?
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
It's a question of scope. iVars and properties declared in the header are visible to anybody that includes the header. iVars and properties declared in an "anonymous category" in the .m file are only visible in that file.

Declaring things in the .m file is the rough equivalent to "private" scope in Java or C++. However, with introspection and KVC, nothing is truly private in Objective C.

The problem with putting iVars in the .m file is that then they are not visible to subclasses of the class you re defining.

What I do is to use iVars in the .h file for protected variable storage, and never access it from outside. I then use properties for publicly available storage.


Hi, I have seen instance variable being declared in various places such as in the header file, in the implementation file etc.

Image

Image

Image

What would be the best practice for declaring instance variables?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.