如何解决是此代码正确的重新存储管理-用于基本的自定义类
| 我是否需要在此处添加/修改任何内容以重新自定义类的内存管理? (例如,是否需要任何\“ release \”行,是否不需要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
解决方法
不它不是。通过将属性声明为
(retain)
,您已使startDate
参数具有retained
。这意味着您有时需要承担4英镑的责任。您可以通过添加以下内容解决此问题:
- (void)dealloc {
[_startDate release];
[super dealloc];
}
另外,您不应在初始化方法名称中使用大写的“ Working”和“ CoreWeekend”。它们应分别为“正在工作”和“ coreWeekend”。
,您需要实现-dealloc
并在其中释放startDate
。否则,这似乎很好。
- (void)dealloc {
[_startDate release];
[super dealloc];
}
,取消分配类后,需要释放_startDate。由于您具有属性,因此最安全的做法是将其设置为nil,自动生成的setter会为您释放它。
- (void)dealloc
{
self.startDate = nil;
[super dealloc];
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。