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?
Yes you should release the strings because when using the properties that retain objects.
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.
Since the
copymethod 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.