2013-05-13 51 views
2

我有这个类在我的项目:的dealloc自定义对象的

 @interface VideoItem : NSObject <NSCoding> { 
       NSString *name; 
       NSString *artist; 
       int seconds; 
     } 

     @property (nonatomic, retain) NSString *name; 
     @property (nonatomic, retain) NSString *imgUrl; 
     @property (nonatomic, retain) NSString *artist; 

     @end 

这也是我如何创建该对象:

VideoItem *item = [[VideoItem alloc] init]; 
    item.name = name; 
    item.imgUrl = imgLink; 
    item.artist = artist; 

这是的dealloc:

- (void)dealloc{ 
    [name release]; 
    [imgUrl release]; 
    [artist release]; 

    [super dealloc]; 
} 

我想知道这dealoc是否可以用NON-ARC?我是否需要执行其他操作,因为NSString与Property?

编辑

而如果VideoItem对象与创造:

VideoItem *item = [[VideoItem alloc] init]; 
     item.name = [NSString alloc][email protected]"%@",name]; 
     item.imgUrl = [NSString alloc][email protected]"%@",imgLink]; 
     item.artist = [NSString alloc][email protected]"%@",artist]; 

在这种情况下做的dealloc的仍然是确定?或者我需要改变一些东西?

回答

2

一切都看起来不错,你正在释放你的对象的所有@properties。我可能会以及它们指向零,只是为了确保,如果这些属性之一被调用时,它将被nilled并没有一个垃圾值,就像这样:

- (void)dealloc{ 
    [name release], name = nil; 
    [imgUrl release], imgUrl = nil; 
    [artist release], artist = nil; 

    [super dealloc]; 
} 

另一件事,没有相关的,这将是清洁的,如果你想创建自己的初始化,这样你就可以通过属性值,当你真正创建对象,像这样:

-initWithName:(NSString *)name withImgURL:(NSString *)imgURL withArtist:(NSString *)artist; 

您编辑:

item.name = [NSString alloc][email protected]"%@",name]; 
item.imgUrl = [NSString alloc][email protected]"%@",imgLink]; 
item.artist = [NSString alloc][email protected]"%@",artist]; 

仅基于此,它会产生泄漏,所以您应该小心。为了解决这个问题:

item.name = [[NSString alloc][email protected]"%@",name] autorelease]; 
item.imgUrl = [[NSString alloc][email protected]"%@",imgLink] autorelease]; 
item.artist = [[NSString alloc][email protected]"%@",artist] autorelease]; 
+0

我编辑我的问题与其他一些东西,并感谢您的帮助! – MTA 2013-05-13 08:34:17

+0

我确实检查了你的编辑并添加了一些信息。 – Peres 2013-05-13 08:35:40

0

如果您没有启用ARC,那么您的析构函数是正确的。您将释放所有保留的属性并调用super,这就是您所需要的。