1

To me, it seems that there are quite a few ways to declare instance variables using Objective C and I'm not sure what is the standard way(s) I should approach this. What are the differences between the following?

I saw this in the new SpriteKit templates:

@implementation GameScene { //instance variables? private?
    SKShapeNode *_spinnyNode;
    SKLabelNode *_label;
}

- (void)didMoveToView:(SKView *)view {
    // Setup your scene here

...

This is what I currently use:

@interface GameScene()

@property SKShapeNode *spinnyNode;

@end

@implementation GameScene

- (void)didMoveToView:(SKView *)view {
    // Setup your scene here

I've also seen the use of @synthesize in certain online examples. I come from more of a Java background where it seems so much more clear. What are the differences?

02fentym
  • 1,762
  • 2
  • 16
  • 29
  • 3
    Plenty of material throughout the site to catch up on: https://stackoverflow.com/questions/11478038/reason-to-use-ivars-vs-properties-in-objective-c, https://stackoverflow.com/questions/3074248/do-declared-properties-require-a-corresponding-instance-variable, etc. **tl;dr**: The first version explicitly declares instance variables (if in the `@interface`, they'll be publicly visible even if not accessible; in `@implementation` they are effectively private), the second uses properties. You should generally prefer properties where possible, and no need for `@synthesize` anymore. – Itai Ferber Dec 31 '16 at 02:03
  • It seems like the first link you posted has older information since we no longer use `@synthesize` anymore right? Good background info though. Is it just me or does Apple constantly change the way that variables are created? – 02fentym Dec 31 '16 at 05:02
  • Does introducing Swift count as "constant change"? ;) Cool profile image btw. – jtbandes Dec 31 '16 at 06:34
  • Lol...nah, I don't think so. I think swift just uses car everywhere. Objective C seems so much more complicated, but I can't move to swift just yet. – 02fentym Dec 31 '16 at 06:35
  • @02fentym I wouldn't say "constantly" — properties were introduced as a way to cut down on the boilerplate of defining ivars, their getters and setters, and the KVC code associated with that. `@synthesize` was a short intermediate stage to help transition existing codebases (though it certainly still has its uses today). Properties have been around for years, though, and have not changed behavior significantly since... – Itai Ferber Dec 31 '16 at 06:58
  • I see. So it seems that simplification in syntax has been the driving factor to these changes. So you had mentioned that using `@interface` allows members to be visible yet private. What's the benefit to having them visible when they're private vs. the `@implementation` method that just makes them private and invisible? – 02fentym Dec 31 '16 at 08:04
  • 1
    @02fentym It depends on how much information you want to present in the public interface. As a general rule of thumb, you rarely want to expose ivars directly; instead, expose only the properties you want to be public, and the rest can go in the `.m` file (you can have private properties declared there too, in a `@interface MyClassName () /* private property declarations */ @end`0. Basically, it's rarely helpful, and generally just a holdover from when that was the only place you could declare ivars. `@implementation` ivars came with properties. – Itai Ferber Dec 31 '16 at 08:23
  • Yeah, that's my basic strategy when I use other languages. Ok, that certainly clears it up for objective C. Thank you so much for all of your responses. Please post an answer and I'll be sure to select it – 02fentym Dec 31 '16 at 08:25
  • 1
    @02fentym Since Objective-C is an incredibly dynamic language, none of this prevents anyone from accessing or messing with your ivars at runtime anyway, but this is about good encapsulation practices and application of proper OOP. To sum up: stick to properties when possible; properties that should be public go in `.h`, private ones go in `.m` in a secondary `@interface`. If you really need ivar control, those should go in the `.m` as well, as needed. – Itai Ferber Dec 31 '16 at 08:25
  • @02fentym I can't, since this was marked as a duplicate, but I am glad to have helped nonetheless. Hopefully others who have the same question will stumble on this in the future too. – Itai Ferber Dec 31 '16 at 08:26
  • Yeah, I disagree that this is a duplicate of the question that rmaddy suggested, but whatever. Thanks anyways. – 02fentym Dec 31 '16 at 08:27

0 Answers0