I’ve made a container class to store a single tweet. Its initialized by passing in a dictionary object which is a single tweet.
I then store an array of these ‘tweets’ which I process through to display in a table.
The project is now finished and I am reviewing everything at the moment and I was wondering is there a better way to do this in the future. Is the memory handled correctly. I declare the string member vars with ‘copy’ and later in the dealloc I use a ‘release’ rather than just setting them to ‘nil’.
Is my init ok or could that be improved?
Tweet.h
#import
@interface Tweet : NSObject
{
NSString * _userName;
NSString * _tweetText;
NSString * _tweetURL;
}
@property (nonatomic, copy) NSString * userName;
@property (nonatomic, copy) NSString * tweetText;
@property (nonatomic, copy) NSString * tweetURL;
- (id) initWithDict:(NSDictionary *)productsDictionary;
@end
Tweet.m
@implementation Tweet
@synthesize userName = _userName;
@synthesize tweetText = _tweetText;
@synthesize tweetURL = _tweetURL;
- (id) initWithDict:(NSDictionary *)productsDictionary
{
NSDictionary *aDict = [productsDictionary objectForKey:@"user"];
self.userName = [aDict objectForKey:@"screen_name"];
self.tweetText = [productsDictionary objectForKey:@"text"];
NSRange match;
match = [self.tweetText rangeOfString: @"http://"];
if (match.location != NSNotFound)
{
NSString *substring = [self.tweetText substringFromIndex:match.location];
NSRange match2 = [substring rangeOfString: @" "];
if (match2.location == NSNotFound)
{
self.tweetURL = substring;
}
else
{
self.tweetURL = [substring substringToIndex:match2.location];
}
}
else
{
self.tweetURL = nil;
}
return self;
}
-(void) dealloc
{
[self.tweetText release];
[self.tweetURL release];
[self.userName release];
[super dealloc];
}
@end
Many Thanks,
Code
At first sight, I see no inherent flaws here. That looks fine. I would prefer to do:
But what you do is good as well.