0

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

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
  • You declare ivars inside the braces and properties outside. Your second example would produce a syntax error by declaring the properties inside the braces. – Mick MacCallum Apr 16 '14 at 03:01
  • Was the response from the other person (nmh) correct? – user3525783 Apr 16 '14 at 03:06
  • Originally, the code in the example had A and B as labels and C was a text field. The labels were inside the braces while the text field was outside (like in the second link, except A and B were labels). I was changing it so A and B became text fields so I am not sure I get why that is going to create a syntax error for those to be outside the braces. Are you saying a label is an ivar but a text field is a property? Why is that the case? – user3525783 Apr 16 '14 at 03:08
  • I think you should take a look at this: http://stackoverflow.com/questions/719788/property-vs-instance-variable – Mick MacCallum Apr 16 '14 at 03:10
  • Setters and getters just set or get an ivar's value...what is direct access? – user3525783 Apr 16 '14 at 03:16

2 Answers2

0

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
nmh
  • 2,497
  • 1
  • 15
  • 27
0
@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) { ... }
sCha
  • 1,454
  • 1
  • 12
  • 22