The following class
declaration seems to me alright. The 'name
' property is deallocated in dealloc
. self.name
always release
retain
properly. But still Xcode analyzer gives warning about leaking. So is this style is not right?
Do I need to initialize any [[init] alloc]
with autorelease
as,
self.name = [[[NSString alloc] init] autorelease];
is it logical? If I use autorelease here, then why to release in dealloc. Even then I do not need to declare the property as (retain), isn't it?
-
@interface Student : NSObject
@property (nonatomic, retain) NSString *name, *address;
@end
-
@implementation Student
-(id)initWithName:(NSString *)name
{
self = [super init];
if (self) {
self.name = [[NSString alloc] init]; // warning here ...
// self.name = @"MyName";
}
return self;
}
- (void)dealloc{
[_name release];
[super dealloc];
}
@end
The what is the best way to it? This way?
NSString _n = [[NSString alloc] init];
self.name = _n;
[_n release];