0

What is the right method of declaration if I want to use single object in my viewcontroller? to use a @property i my .m file

@property (nonatomic, strong) UITextView *resolutionText;
@property (nonatomic, strong) AWLResolutionView *myView;

or to declare them in my .h file

@interface 
{
@private
    UITextView *_resolutionText;
    AWLResolutionView *myView;
}
dav
  • 5
  • 3
  • The first (declaring it on your .m file). The latter is the old version when there were no anonymous categories :) – HAS Jul 02 '14 at 07:44
  • 1
    Also note that **nothing** in Objc is **really** private. – HAS Jul 02 '14 at 07:45
  • This could be useful.. http://stackoverflow.com/questions/4903651/is-there-any-reason-to-declare-ivars-if-youre-using-properties-exclusively-in-o – Amar Jul 02 '14 at 07:47
  • Thanks for everyone for the answer! – dav Jul 02 '14 at 08:01

2 Answers2

0

For the sake of clean coding I would prefer creating properties in the anonymous category inside the .m file.

However, using @property creates automatically an instance variable for you that has the same name as your property preceded by an underscore (_), that can be accessed from within the .m file. This is called synthesising. Y ou can also manually synthesize a property to a custom instance variable using @synthesize.

Apple provided some clear instructions how to write clean code in their developer library.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
Quxflux
  • 3,133
  • 2
  • 26
  • 46
  • Just to add. Auto synthesis has a few exceptions where it doesn't work. For instance in `NSManagedObject` subclasses. Or if you override both the getter and setter methods of the property. – Fogmeister Jul 02 '14 at 08:02
0

The best way to declare the private variable should be declared as @property in the extension of .m file . If you see in your .m file there is an extension class called as @interface by default, so declared the same in the extension class. Also no need of writing extra code in.h file for declaring the private variable.

So your first approach is best.

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56