Question

I only declare a variable (i.e. NSString) as @property and @synthesize, I never declare it in the interface class, example below:

.h file:

@interface myViewController : UIViewController {
}

@property (nonatomic,retain) NSString *text;

It is working fine with me, so should I declare it in myViewController class? and why?

P.S. ARC is enabled.

Was it helpful?

Solution

long time ago, you should do it like this:

@interface myViewController : UIViewController {
    NSString *_text;
}

@property (nonatomic,retain) NSString *text;

@synthesize text = _text;

but now, you can instead of it:

@interface myViewController : UIViewController {
}

@property (nonatomic,retain) NSString *text;

It's same, the IDE do it for you,and the also @synthesize properties for you with the Xcode4.5 . No one could tell you which way you should do,but could be clear what you want to do with your code, and then, do whatever with it.

OTHER TIPS

There's no specific reasons for declaring variables instead of properties (declaring both is just useless).

I tend to declare just properties as you do, since I repute bad practice to manipulate instance variables directly in the implementation (with rare exceptions like init methods) and I like to use getters and setters.

Also properties have the great advantage of being easily customizable in terms of behavior: handling thread-safety, visibility and memory management is just a matter of a few keywords.

You meant in the interface section in .m file?

The properties declared in the interface section of .m file of UIViewController are just for private use and you can't access it from other classes, whereas properties declared in the interface section of .h file are class properties and are accessible out of the class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top