Colas
Colas

Reputation: 3573

Should outlets have getters/setters?

Should I better always write

    @interface MyClass
    {
        NSTextField * myTextField ;
    }
    @property (assign)  NSTextField * myTextField ;

or just

    @interface MyClass
    {
        NSTextField * myTextField ;
    }

?

Why I have reasons to think that we should add the @property line :

Upvotes: 2

Views: 708

Answers (3)

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

As soon as you create a property for an outlet object or any other object then ivar is created for it (in new compilers).

Now creating ivars and properties are obsolete. Even property are matched up with auto-synthesizes.

If you really want ivars then you can put it in .m file or if you want them to be private then add an extension to the class with them.

Should outlet have setter/getter

Yes indeed you need to access them many times. Like checking the state of Radio button, check box, setting stringValue is NSTextField, reloading table by [tableView reloadData] etc.

@interface MyClass
    {
        NSTextField * myTextField ; //this is ivar
    }
    @property (assign)  NSTextField * myTextField ; //this is property
@end

Whatever you put in .h is public and accessible, if you want to make it hidden do it in .m even within an extension.

Upvotes: 3

Mrunal
Mrunal

Reputation: 14118

I personally prefers, if you require your object reference scope, outside the class then go with property declaration. Otherwise declare it as instance variable inside the class brackets and that to in implementation file. (if you are not inheriting this variable feature further)

So,

  • If you require to access your variable instance outside the class then declare property. In MyClassViewController.h header file

@property (strong, nonatomic) IBOutlet NSTextField *myTextField;

So now in other classes, one can access this by:

MyClassViewController *myClassViewController = [[MyClassViewController alloc] init];
[myClassViewController.myTextField setText:@"MyValue"];

also get value like

NSLog(@"%@", [myClassViewController.myTextField text]);
  • If you don't want such public access, but you want it to get inherited. Then declare in class declaration block itself. In ViewController.h header file

    @interface ViewController { @public: // or @protected
      NSString *myString;
    }
    

Now if there is another class which inherits ViewController class then this property will get inherited in ChildViewController according to its access specifier.

  • If you want completely hidden property or variable. Like its not even being used outside of that class itself, then you can declare it in implementation file (.m) in private category itself. In ViewController.m file:

@interface ViewController () {

  NSString *myString;
}

So this will be having visibility in .m file only, nowhere else.

Now declaration of @property- You require to declare this only when you require setter-getter default methods (Like setMyString etc). This also you can declare in both .h as well in .m according to your visibility requirement of that instance.

Hope now you are getting the concept. Its all about visibility.

Upvotes: 1

Ryan Poolos
Ryan Poolos

Reputation: 18551

All you need is the property for Interface Builder using IBOutlet. If you're not using Interface Builder you don't need IBOutlet. If you're not using Interface Builder you may not even need a property at all (properties are generally public, while instance variables are always private).

@interface MyClass : UIViewController

@property (assign) IBOutlet NSTextField *myTextField ;

You don't need to create an instance variable or synthesize it as its done for you.

You can access it with self.myTextField in your code.

Upvotes: 1

Related Questions