views:

187

answers:

3

Is it bad to create @properties for private variables just for the memory management benefits?

It seems messy and wrong to have public facing @properties for many private variables.

(Mainly, I am releasing private ivars during low memory conditions using the respective "event" methods.)

Example: I usually do this to release a private ivar:

[name release]; name = nil;

But with @properties, I can do this:

self.name = nil;

Later in my code, will do this, hence the need to set to nil:

if( !name)
    name = [[NSString alloc] initWithFormat:@"Hi %@",inputName];
A: 

For public properties I don't think Apple recommends it, because sometimes setting a property to nil can have side effects other than just releasing the variable (KVO notifications, or a custom setter method that does something else).

As for private properties, I'm not too sure. Using a property will only save you a couple of key strokes while coding, but you're also making it slightly more complex and fragile. I favour readability and maintainability over convenience to write, because you'll save time in the long run.

Tom Dalling
Quote from apple documentation in UIViewController Class reference for method - (void)viewDidUnload: "The preferred way to relinquish ownership of any object (including those in outlets) is to use the corresponding accessor method to set the value of the object to nil"
bentford
I looked around to find the original reason why I thought it was bad (because I forgot), and I found this: http://stackoverflow.com/questions/192721/why-shouldnt-i-use-obective-c-2-0-accessors-in-init-dealloc . It seems that it's only a concern in init or dealloc.
Tom Dalling
+2  A: 

Properties exist for your convenience. If you don't want other people to use properties that exist in your classes, just don't document them.

NSResponder
But they are visible in your .h file. andyvn22 has a alternative solution to this.
bentford
+14  A: 

An alternative is to keep the property private. You can use the following code (in your .m file) to make the property only accessible within your class:

#import "MyClass.h"

@interface MyClass ()
    @property (retain) NSString* privateString; 
@end

@implementation MyClass

    @synthesize privateString;
    // Your code here

@end

Now you've got the ease of a property, but other classes still can't access it, even if they import your .h file!

andyvn22
This is great, but it seems like a hack. I'm not sure I'm going to do this in my code, but at least I know there is an alternative to what I'm doing now. Thank you!!
bentford
Its not a hack, it is an explicit form of Category called an Extension. <https://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCategories.html#//apple_ref/doc/uid/TP30001163-CH20-SW2>. "It is common for a class to have a publicly declared API and to then have additional API declared privately for use solely by the class or the framework within which the class resides"
Peter N Lewis
It is absolutely not a hack and is 100% in line with why class extensions were added to the language in the first place. Embrace this pattern with gusto.
bbum
Got it. I will start using this then.
bentford