Iphone 自定义对象的解除锁定
我在我的项目中有这样的课程:Iphone 自定义对象的解除锁定,iphone,ios,objective-c,Iphone,Ios,Objective C,我在我的项目中有这样的课程: @interface VideoItem : NSObject <NSCoding> { NSString *name; NSString *artist; int seconds; } @property (nonatomic, retain) NSString *name; @property (non
@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
这是dealloc:
- (void)dealloc{
[name release];
[imgUrl release];
[artist release];
[super dealloc];
}
我想知道这个dealoc
是否适合非ARC
?我是否需要执行其他操作,因为此NSString
具有属性
编辑
如果创建VideoItem对象时使用:
VideoItem *item = [[VideoItem alloc] init];
item.name = [NSString alloc]initWithFormat@"%@",name];
item.imgUrl = [NSString alloc]initWithFormat@"%@",imgLink];
item.artist = [NSString alloc]initWithFormat@"%@",artist];
在这种情况下,解除锁定是否仍然正常?或者我需要更改一些内容?如果没有启用ARC,则析构函数是正确的。您正在释放所有保留的属性并调用super,这就是您所需要的。一切正常,您正在释放对象的所有
@properties
。我可能会将它们指向nil,只是为了确保,如果调用其中一个属性,它将为nill,并且没有垃圾值,如下所示:
- (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;
另一件事,不相关,如果您创建自己的init,它会更干净,这样您可以在实际创建对象时传递属性值,如下所示:
- (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]initWithFormat@"%@",name];
item.imgUrl = [NSString alloc]initWithFormat@"%@",imgLink];
item.artist = [NSString alloc]initWithFormat@"%@",artist];
仅基于此,它将产生泄漏,因此您应小心。要解决此问题,请执行以下操作:
item.name = [[NSString alloc]initWithFormat@"%@",name] autorelease];
item.imgUrl = [[NSString alloc]initWithFormat@"%@",imgLink] autorelease];
item.artist = [[NSString alloc]initWithFormat@"%@",artist] autorelease];
我用其他东西编辑了我的问题,谢谢你的帮助!!