1

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.

jscs
  • 63,694
  • 13
  • 151
  • 195

1 Answers1

2

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • OK, so why do you think they made it weak? – asdfkjaasdflf May 25 '17 at 17:54
  • Great question. I have no good answer. Either habit/reflex, or (seems unlikely) they expect it to go away at some point but not to be set again after that, and want the auto-nil behavior. – jscs May 25 '17 at 18:07