Reputation: 7900
I have the Header of this class in my project:
@interface VideoItem : NSObject <NSCoding> {
NSString *idStr;
NSString *name;
NSString *link;
}
-(id)initWithVideoItem:(VideoItem*)video;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *link;
@property (nonatomic, retain) NSString *idStr;
@end
this is the implement:
@implementation VideoItem
@synthesize name,link,idStr;
-(id)initWithVideoItem:(VideoItem*)video{
if (self = [super init]) {
self.name = video.name;
self.link = video.link;
self.idStr = video.idStr;
}
return self;
}
#pragma mark
#pragma mark NSCoder
- (void)encodeWithCoder:(NSCoder *)encoder{
[encoder encodeObject:self.name forKey:@"video_name"];
[encoder encodeObject:self.link forKey:@"video_link"];
[encoder encodeObject:self.idStr forKey:@"video_id"];
[encoder encodeObject:self.imgUrl forKey:@"video_img"];
[encoder encodeObject:self.viewCount forKey:@"video_views"];
[encoder encodeObject:self.artist forKey:@"video_artist"];
[encoder encodeObject:self.timeStr forKey:@"video_timestr"];
[encoder encodeInt:self.seconds forKey:@"video_secondes"];
[encoder encodeInt:self.rating forKey:@"video_rating"];
[encoder encodeObject:self.pubDate forKey:@"pubDate"];
}
- (id)initWithCoder:(NSCoder *)decoder {
if(self = [super init]){
self.name = [decoder decodeObjectForKey:@"video_name"];
self.link = [decoder decodeObjectForKey:@"video_link"];
self.idStr = [decoder decodeObjectForKey:@"video_id"];
}
return self;
}
@end
And i want to know if in case like this i need to add dealloc
method and release the strings or not?
Upvotes: 0
Views: 92
Reputation: 1494
In fact you need not call the release on the ivars. Instead use the properties self.ivar = nil. This releases your memory and sets the pointes to nil as a result of it there are no dangling pointers.
if you use [ivar release], ivar is released but is a dangling pointer, so most of the time ivar = nil; is done after releasing the ivar.
- (void)dealloc
{
self.idStr = nil;
self.name = nil;
self.link = nil;
[super dealloc];
}
Upvotes: -1
Reputation: 69499
Yes you should release the strings because when using the properties that retain objects.
- (void)dealloc {
[idStr release];
[name release];
[link release];
[super dealloc];
}
Normally you would copy the object in the init, this is a better way then since the orginal object can then savely be edited or released.
-(id)initWithVideoItem:(VideoItem*)video{
if ((self = [super init])) {
name = [video.name copy];
link = [video.link copy];
idStr = [video.idStr copy];
}
return self;
}
Since the copy
method return a retained object you want to skip the property, since that would increase the retain count.
On a other note: the objective-c convention the private ivar should start with an _
to make it more obvious that they are not properties.
Upvotes: 1
Reputation: 107231
You are retaining your string properties. So it's your task to release it.
So add a dealloc
method and release them.
- (void)dealloc
{
[idStr release];
[name release];
[link release];
[super dealloc];
}
Upvotes: 0
Reputation: 248
you have to write - (void)dealloc Method as you are retaining the variable's. and release these variables in this method.
Upvotes: 0
Reputation: 46563
Use this, release all your allocated variables and at last call super dealloc :
- (void)dealloc{
[idStr release];
[name release];
[link release];
[super dealloc];
}
Upvotes: 0
Reputation: 1120
Use ARC and forget any problems related to memory management. Even apple encourages using ARC whenever possible. If you are doing this as a new development, I would recommend you to use ARC.
In case you do not want to use ARC, you need to implement dealloc and release your member variables.
Upvotes: 1