Question

I have a couple of examples of code. What is the difference between defining the two text fields (A and B) inside the @interface ViewController {} and defining the two text fields outside of it? Thanks.

http://pastie.org/9083686

http://pastie.org/9083687

Was it helpful?

Solution

@interface ViewController : UIViewController {
    UITextField *a;
}

@property (nonatomic, strong) UITextField *b;

both a and b are iVars, but a does not have implicitly created setter/getter by default.
You can access a by doing something like

if (a.text.length == 0) { ... }

and we call it direct access.

but for b, we use self. to have an access to it.

if (self.b.text.length == 0) { ... }

By using self., you are stating that you want to use setter/getter method to access b.

You can even choose not to use setter/getter by accessing _b (if you have not explicitly synthesized it) directly like below:

if (_b.text.length == 0) { ... }

OTHER TIPS

Internal class ViewController can access to a

@interface ViewController : UIViewController{
   UITextField *a; 
}

Outside class ViewController can access to a

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *a;
@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top