iphone - 这是正确的代码重新存储管理-对于基本的自定义类

标签 iphone objective-c ios memory-management

我是否需要在此处添加/修改任何内容以重新自定义类的内存管理? (例如,需要任何“发布”行,不需要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/

相关文章:

iphone - 清理 Localized.strings

iphone - 关键帧动画期间应用程序崩溃 : -[NSConcreteValue doubleValue] unrecognized selector

objective-c - 如何将 CVImageBufferRef 转换为 UIImage

iOS:使用尺寸类别的 ios 应用程序所需的最少图像资源

ios - "Received memory warning"在 ios 中使用 NSThread 时

ios - 为UIPickerView didSelectRow返回NSArray中字符串的第一个字符

ios - Swift:检查有一个协议(protocol)并像在 Java 中一样使用 getter

iphone - iOS 4.2 - 使用 Apple AirPrint 打印 DOC、PPT、XLS 等?

ios - numberOfRowsInSection 方法调用了两次

objective-c - iPad 启动方向