Obj-C, properties for everything

I started working in a new company, and one of the recommendations that my team told me to follow was to rarely use save / release and instead rely on properties to manage memory. I see how clear it is that the code remains clear, and leaving less room for errors, but opening such interfaces makes me uncomfortable. Generally speaking, the architecture is very good, but I was always pedantic in closing my classes in the outside world.

Is using such properties an acceptable design methodology in objective-c? Can someone provide me with links or a hint where my new team may have chosen this strategy?

+4
source share
2 answers

No need to expose properties to the whole world. In your .m implementation file, you can add a small category to declare 'private' properties. For instance.

#import "Class.h" @interface Class () @property (nonatomic, strong) NSDate *privateProperty @end @implementation Class @synthesize privateProperty; ... @end 

Nothing in Objective-C is really confidential in strict terms, so I would say it was a good practice - it hides almost all of the save / release material without requiring ARC compatibility and has a side effect of not requiring you to specify at all your instance variables in the header (although there are other ways to achieve this).

As a historical note, I think this was the first way to move instance variables from the header - something is only allowed by the "new" runtime on iOS and 64-bit Intel 10.6+ - so this may be the secondary reason why your team dealt with by this. Unless they explicitly told you to make your classes transparent, they can actually fully match your feeling (and the well-accepted object-oriented principle) that implementations should be opaque.

+10
source

You do not need to publicly declare your objects. Using a class or class extension, you can put your objects in an implementation.

For instance:

 // in AnObject.h @interface AnObject : NSObject @end // in AnObject.m @interface AnObject () // () is class extension, (foo) is a class category @property (retain) NSString *foo; @end @implementation AnObject @synthesize foo; @end 

For more information, see the Apple documentation.

+2
source

Source: https://habr.com/ru/post/1392729/


All Articles