Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic iVars with @synthesize

I understand that starting with iOS 4, there is now the ability to not declare iVars at all, and allow the compiler to automatically create them for you when you synthesize the property. However, I cannot find any documentation from Apple on this feature.

Also, is there any documentation on best practices or Apple recommended guidelines on using iVars and properties? I have always use properties like this:

.h file

@interface myClass {
    NSIndexPath *_indexPath
}

@property(nonatomic, retain) NSIndexPath *indexPath

@end

.m file

@implementation myClass

@synthesize indexPath = _indexPath;

- (void)dealloc {
    [_indexPath release];
}
@end

I use the _indexPath instead of indexPath as my iVar name to make sure that I don't ever use indexPath when I need to use self.indexPath. But now that iOS supports automatic properties, I don't need to worry about that. However, if I leave out the iVar declaration, how should I handle releasing it in my dealloc? I was taught to use iVars directly when releasing in dealloc, rather than using the property methods. If I don't have an iVar at design-time, can I just call the property method instead?

like image 475
GendoIkari Avatar asked Mar 17 '11 18:03

GendoIkari


3 Answers

I've went through many different ways of dealing with this. My current method is to use the property access in dealloc. The reasons not to are too contrived (in my mind) to not do it, except in cases where I know the property has odd behavior.

@interface Class
@property (nonatomic, retain) id prop;
@end

@implementation Class
@synthesize prop;

- (void)dealloc;
{
    self.prop = nil;
    //[prop release], prop=nil; works as well, even without doing an explicit iVar
    [super dealloc];
}
@end
like image 159
Joshua Weinberg Avatar answered Oct 17 '22 15:10

Joshua Weinberg


In constrast, I do the following:

@interface SomeViewController : UIViewController

@property (nonatomic, copy) NSString *someString;

@end

and then

@implementation SomeViewController

@synthesize someString;

- (void)dealloc
{
    [someString release], someString = nil;
    self.someString = nil; // Needed?

    [super dealloc];
}

@end

Note: At some point Apple will enable synthesize-by-default which will no longer require the @synthesize directive.

like image 26
Mark Grimes Avatar answered Oct 17 '22 13:10

Mark Grimes


You can directly access instance variables using -> symbol instead of dot . (which will invoke ivar's corresponding accessor method):

.h

@interface myClass {
}
@property(nonatomic, retain) NSIndexPath *indexPath

@end

.m

@implementation myClass

- (void)dealloc {
    [self->indexPath release];
    self->indexPath = nil; // optional, if you need it

    [super dealloc];
}
@end

Thus you will directly access iVar and not it's corresponding accessor method, obtaining additional benefit - performance.

like image 20
Martin Babacaev Avatar answered Oct 17 '22 13:10

Martin Babacaev