Linus
Linus

Reputation: 4783

.h instance variable declaration

I'm having a hard time understanding why the following textfield is declared twice in some tutorials.

In the .h file:

# include <UIKit/UIKit.h>

@interface MyViewController : UIViewController {

    UITextField *name;  // <----- What do I need this for? Is it the same as below?
}

@property (nonatomic, strong) IBOutlet UITextField *name;  // <----- Same as this?

@end

At first I thought this would be something like an instance variable, but they are only declared here in the .m file, right?

.m file

#import "MyViewController.h"

@implementation UIViewController {

    NSString *myString; // <----- This is an instance variable, right?

}

What's the "UITextField *name;" for? Don't I only need the second one with the @property in front? Thank you.

Upvotes: 1

Views: 192

Answers (2)

LE SANG
LE SANG

Reputation: 11005

This is an old way, just use property is OK. If you declare both, you must use @synthesize name; in your .m file to make self.name same as name. XCode4.2 auto synthesize name = _name. So use self.name as much as possible in your .m file.

Variable in {} just use for internal or private, when you don't want implement setter and getter.

Upvotes: 1

Parag Bafna
Parag Bafna

Reputation: 22930

If you are targeting iPhone OS or 64-bit Mac OS X then you do not need to define ivars for your properties. Take a look at Dynamic ivars: solving a fragile base class problem

Upvotes: 1

Related Questions