Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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];
like image 263
bentford Avatar asked Sep 10 '09 23:09

bentford


1 Answers

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!

like image 123
andyvn22 Avatar answered Oct 05 '22 23:10

andyvn22