xCode iPhone self initiating variables.
lets say, I have a variable defined in .h, before using it on the .m file I have to initiate it, normally the initiating code is inserted some where on viewDidLoad if the variable is used through out the .m, however the dis-advantage with this format is that the variable will be initiated all the time the view is loaded, even when the variable is never used.
however with the following method, the variable is initiated on the first instance that its actually referred, there by if the variable is not used in a specific run the variable wont be initiated.
on the .h file
@interface ... {
CLLocationManager *locMan;
.
.
.
}
.
.
.
@property (nonatomic, retain) CLLocationManager *locMan;
.
.
.
@end
on the .m file
-(CLLocationManager *)locMan{
if (locMan != nil) {
return locMan;
}
locMan = [[CLLocationManager alloc] init];
locMan.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
locMan.delegate = self;
return locMan;
}

0 Comments:
Post a Comment
<< Home