Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obj-C, properties for everything

I have started work at a new company and one of the guidelines I have been told to adhere to by my team lead is to rarely use retain/release and instead rely on properties for memory management. I can see the appeal of keeping the code clear and leaving less room for mistakes but opening up the interfaces like this makes me uncomfortable. Generally speaking the architecture is very good but I have always been pedantic about closing up my classes to the outside world.

Is using properties like this an accepted design methodology in objective-c? Can anyone provide me with links or a clue where my new team may have picked up this strategy?

like image 919
Sam Avatar asked Dec 16 '22 04:12

Sam


1 Answers

There is no need to expose properties to the entire world. In your implementation .m file you can add a little category to declare 'private' properties. E.g.

#import "Class.h"

@interface Class ()
@property (nonatomic, strong) NSDate *privateProperty
@end

@implementation Class

@synthesize privateProperty;

...
@end

Nothing in Objective-C is ever really private in strict terms, so I'd say this was good practice — it hides almost all of the retain/release stuff without requiring an ARC-compatible runtime and has the side effect of not requiring you to mention your instance variables in the header at all (though there are other ways to achieve that).

As a historical note, I think this was the first way to move instance variables out of the header — which is something permitted only by the 'new' runtime on iOS and 64bit Intel 10.6+ — so that may be a secondary reason why your team have settled upon it. Unless they've explicitly told you to make your classes transparent, they may actually be completely in agreement with your feeling (and the well accepted object oriented principle) that implementations should be opaque.

like image 83
Tommy Avatar answered Dec 27 '22 07:12

Tommy