do I need to add/modify an开发者_运维技巧ything here re memory management for a custom class? (e.g. any "release" lines required, don't need a dealloc method?)
#import <Foundation/Foundation.h>
@interface TimelineItem : NSObject {
NSDate *_startDate;
BOOL _working;
BOOL _coreWeekend;
}
@property (nonatomic, retain) NSDate *startDate;
@property (nonatomic) BOOL working;
@property (nonatomic) BOOL coreWeekend;
- (id)initWithStartDate:(NSDate*)startDate Working:(BOOL)working CoreWeekend:(BOOL)coreWeekend;
@end
#import "TimelineItem.h"
@implementation TimelineItem
@synthesize startDate = _startDate;
@synthesize working = _working;
@synthesize coreWeekend = _coreWeekend;
- (id)initWithStartDate:(NSDate*)startDate Working:(BOOL)working CoreWeekend:(BOOL)coreWeekend {
if (self == [super init])
{
// Initialization
self.startDate = startDate;
self.working = working;
self.coreWeekend = coreWeekend;
}
return self;
}
@end
No, it is not. You've retained
the startDate
parameter by declaring your property as (retain)
. This means you're responsible for releasing
it at some point. You can fix this by adding:
- (void)dealloc {
[_startDate release];
[super dealloc];
}
Also, you shouldn't be capitalizing "Working" and "CoreWeekend" in the init method name. They should be "working" and "coreWeekend", respectively.
You need to implement -dealloc
and release startDate
there. Otherwise, this appears to be fine.
- (void)dealloc {
[_startDate release];
[super dealloc];
}
You need to release _startDate when the class is dealloced. Since you have as a property, the safest thing to do is set it to nil, and the auto-generated setter will take care of releasing it for you.
- (void)dealloc
{
self.startDate = nil;
[super dealloc];
}
精彩评论