2

I'm getting comfortable with iOS and Objective-C programming, but where instance variables are declared is still a confusing point for me.

  • Most of the time they seem to be declared in the class' .h file, as part of the interface.
  • Other times, they are declared in the .m file's interface.
  • Another way I've seen is when they're declared in the .h file, but in a "separate division" of the interface, for lack of a better term, like so:

    @interface MapViewController : UIViewController {
    
        UIPopoverController *popoverController;
    
    }
    
    ....
    

Where is the correct way to put them? Is it entirely subjective? I've got the impression that for instance variables that other classes will need to access, put them as a main property in the .h file, but for variables that only the class needs to access class-wide, put them in the .m's interface. Then the special division of .h confuses me all together.

Could someone shed some light on this?

Sebastian
  • 7,670
  • 5
  • 38
  • 50
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • 1
    I have to admit: Nobody's asked this question before, but it's probably because you should read [the docs on ObjC](https://developer.apple.com/library/mac/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/). (AAAnd, queue 10 answers that are either very short or very long but don't quite answer the question below) – CodaFi Mar 28 '13 at 03:22
  • 1
    @CodaFi I thought Rob Napier's answer addressed this question. Although I can see how the mapping isn't 100%. If it gets closed and Doug isn't satisfied with the answers from the possible dupe I'll keep an eye on it and make sure to vote to re-open. Thanks for causing me to take a second look. – Carl Veazey Mar 28 '13 at 03:34
  • @CarlVeazey I see no reason why your dupe and the docs shouldn't constitute a dupe of the question. I certainly voted to close (because I honestly tried this question's title ten times and couldn't come up with anything that close). – CodaFi Mar 28 '13 at 03:41

0 Answers0