-2
@interface QuickViewControllViewController : UIViewController<UIScrollViewDelegate>{
IBOutlet UIWebView *myWebView;
UIScrollView *scrollView;

IBOutlet UIImageView *imagePreview;
UIImage *source;

}

@end

What is the differences between these declarations and ones starting with @property if I don't want them exposed outside of the class? Xcode automatically makes them @properties when you command drag them from storyboard. I'd rather not use the @property so I don't have to use self. Am I better off making them properties?

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 2
    I Don't want to be rude but.. Reading an objective-c book? – oiledCode Jun 01 '13 at 20:29
  • http://stackoverflow.com/questions/14236799/should-i-declare-variables-in-interface-or-using-property-in-objective-c-arc/14236931#14236931 – foundry Jun 01 '13 at 20:30
  • I think accessing through self. is a good approach. The setter and getter of properties can provide a choke point for, e.g., lazy instantiation, or other manipulation. If you don't want iVar or properties exposed, declare them in implementation file instead of .h file. – John Jun 01 '13 at 20:34
  • @elio.d as long as someone else may end up asking the same question, it is welcome. You never know when the simplest question will lead to [a non trivial answer](http://stackoverflow.com/a/16664597/412916). But the problem here is not using the search, or look at the suggestion column on the right, so I'm voting to close. – Jano Jun 11 '13 at 23:40
  • @jano infact the problem here is that, from my point of view,for question like this you can find good reference in a good book. But maybe I'm wrong and this is the new way to learn. – oiledCode Jun 11 '13 at 23:43
  • Steering users to the library discourages user participation, which is bad because SO feeds on newbies to build the fastest reference ever for trivial questions (we all have one at some point). As for abuse, SO protects itself with mechanisms like closing, lowering rep, restricting questioning, flagging, etc. At the end of the day, the newbie gets the message, and the [help vampire](http://meta.stackexchange.com/q/19665/162235) is punished. Tomorrow, another newbie will miss the search box, and sighs of despair will be made. Pro tip: Stop Worrying and Love the Bomb. :) – Jano Jun 12 '13 at 01:51

3 Answers3

1

Xcode automatically makes them @properties when you command drag them from storyboard

That's not true, if you want to create class members that are not properties form xib/storyboard, then drag then command drag into the {} section of your .h file and in this way they won't be declared as properties.

@interface QuickViewControllViewController : UIViewController<UIScrollViewDelegate>{
// drag here the line from xib/storyboard for instance members and not properties
}

Now, properties are used when you want to access the members outside your class like instance.property and also for every property declared the system will automatically generate setters and getters which won't happen for the other kind of class members.

So in your case if you want to have only class members that are visible inside your class you can use your posted code and in my opinion it's better not to use properties if you are having class members visible only in one class.

As a note, you can also declare private properties for this case but is kind of useless.

danypata
  • 9,895
  • 1
  • 31
  • 44
0

@property makes it a property,while the other make it an instance variable that is just aaccessible inside the class.Yeah it is good to go declaring instance variable if you dont need any access to it outside the class

When you are creating the outlet by xcode,xcode makes it a property in other words, Properties are supposed to be made that way.You can write in the code then set it in the older ways by right click on the object in IB and drag an outlet to files owner

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
0

Making them an @property is absolutely the the best practice as is using self and the getters and setters. If you want to reduce the exposure, try this in your .m file.

@interface QuickViewControllViewController()

@property (nonatomic, weak) IBOutlet UIWebView *myWebView;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, weak) IBOutlet UIImageView *imagePreview;
@property (nonatomic, strong) UIImage *source;

@end

If you really want to use the ivars directly, you could still do so like _source and _myWebView

Jeff Wolski
  • 6,332
  • 6
  • 37
  • 69