我是否需要在此处添加/修改任何内容以重新自定义类的内存管理? (例如,需要任何“发布”行,不需要dealloc方法吗?)
#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
最佳答案
不它不是。您已经通过将属性声明为retained
来对startDate
参数进行(retain)
。这意味着您有责任在某个时间对其进行releasing
编码。您可以通过添加以下内容来解决此问题:
- (void)dealloc {
[_startDate release];
[super dealloc];
}
另外,您不应该在初始化方法名称中大写“Working”和“CoreWeekend”。它们应该分别是“正在工作”和“coreWeekend”。
关于iphone - 这是正确的代码重新存储管理-对于基本的自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6116796/