asdfkjaasdflf
asdfkjaasdflf

Reputation: 71

Why make an IBOutlet variable an ivar (not a property)?

I've been perusing an open source project that uses a storyboard that contains a few view controllers. For some reason, the UI elements do not use @property IBOutlet declarations in the .h file, but rather are ivar's (sometimes __weak) declared in the .m file like so:

@implementation FunnyScreen {
  __weak IBOutlet SillyButton *bouton;
}

instead of what I've always done and seen other people do:

@interface FunnyScreen: UIViewController
@property (nonatomic,strong) IBOutlet SillyButton *bouton;

Does anyone know why someone would write code like the first example i.e. ivar approach? Does it serve any purpose? I'm keeping an open mind.

Upvotes: 1

Views: 152

Answers (1)

jscs
jscs

Reputation: 64002

The great thing about a synthesized property is that it gives you three things in one concise line of code: storage plus two accessor methods.

But what if you don't need those accessor methods? What if, once the value's set, it's never going to change? And you don't need access outside this class (which is generally a good rule for IBOutlets)?

Well, then you just need storage, which is an ivar declared in the implementation block.

Personally, I often use ivars for exactly that: non-changing internal values -- even in these days of ubiquitous properties. I can't say for certain that's what the author of this code was thinking, but it seems reasonable.

Upvotes: 2

Related Questions