0

Simple question, as I am new to IOS.

What is the difference as to where you declare variables/fields etc.

Option 1 (header file)

@interface ViewController : UIViewController {
UIView *testView;
}

Option 2 (header file)

@interface ViewController : UIViewController
@property UIView *testView;

Option 3 (implementation file)

@implementation ViewController {
UIView *testView;
}
@end
Jano
  • 62,815
  • 21
  • 164
  • 192
StuartM
  • 6,743
  • 18
  • 84
  • 160
  • possible duplicate of [Declaration/definition of variables locations in ObjectiveC?](http://stackoverflow.com/questions/12632285/declaration-definition-of-variables-locations-in-objectivec) – rmaddy Feb 15 '13 at 23:01
  • 1
    Assuming you are using ARC they are all the same. In the third case, the instance variable is not visible for a class importing `ViewController.h`. – Jano Feb 15 '13 at 23:12
  • @Jano no, they are not the same. Options 1 and 3 result in a private ivar. Option 2 defines a public property and a private ivar. – rmaddy Feb 15 '13 at 23:41
  • You should check this good tutorial ;-) http://rypress.com/tutorials/objective-c/properties.html – Vinzius Feb 15 '13 at 23:48
  • uhm ok, but 1 is a protected ivar, which is the default access. – Jano Feb 16 '13 at 01:09
  • I wrote a massive example at http://stackoverflow.com/a/14906215/412916 – Jano Feb 16 '13 at 02:14

2 Answers2

1

There is no real difference, as they all accomplish the same result. But here's the details:

Option 1 vs. Option 2:

Declaring the iVar is redundant, as a property creates it for you. A property's main job is to automatically create accessor methods, (setX and getX) and can furthermore define how these are accessed (atomically or not) or how the value is stored in memory (simply assigned like any ivar? strong or weak?). My recommendation is that you forget about Option 1 and always stick to Option 2 e.g.

@property (nonatomic, strong) UIView *yourView;

--

Option 2 vs. Option 3

(Let's skip the fact that your Option 3 is another iVar and assume you declared a @property here, cause that's more interesting)

There is no difference except for the fact that if you declare the property in your implementation file (.m), others, who include your header, will not see it i.e. it's "kind of private". (Sometimes referred to as 'Private Interface'). As a matter of fact, it's not truly private as one can still call the selector (dynamic nature of obj-c). Nevertheless it's good practice to hide your implementation and only expose what's truly necessary in your header file.

Therefore, use Option 3 for internal stuff, and Option 2 for external stuff.

pkluz
  • 4,871
  • 4
  • 26
  • 40
0

Option 1: This is a member field, with no property accessors (i.e. getter/setter). You have to access it as someInstanceOfViewController->testView (although, inside of ViewController's implementation, this could be shortened to just testView because of the implicit self).

Option 2: This is a public get/set property, which means you access it using someInstanceOfViewController.testView. You can customize several things about a property's getter/setter methods. Among the most commonly used are readonly, retain (meaning that if you set that property to some object, the auto-generated setter method automatically retains that object, and releases the old object that the property contained), and assign (no automatic retain/release semantics). After declaring a property, you can either use the auto-generated getter/setter, or you could write your own, by implementing -(UIView *)testView (getter) and/or -(void)setTestView:(UIView *)testView.

Option 3: A private property, with all the behaviors as option #2, but which the compiler enforces to be only accessible from inside the implementation file.

tom
  • 18,953
  • 4
  • 35
  • 35
  • So to follow on, Option 2 is available publicly throughout the application, whilst Option 3 is only available to that file/class? – StuartM Feb 15 '13 at 23:06
  • Correct. A good rule of thumb is to use Option 2 for publicly accessible properties, and Option 3 for private implementation details. The retain/release semantics are useful enough that you would only use Option 1 in very rare situations. – tom Feb 15 '13 at 23:08
  • And lastly does the same rule apply for all variables being declared like UITextField, IBOutlets etc – StuartM Feb 15 '13 at 23:12