0

Please can anyone try to help me on this issue. I have one requirement like, I want to call a class method from static constructor (+(id)initialize) method like below that will intialize the CLLocationmanager. My problem is how can I give a delegate in that class method and how to get delegates method called.

+(void)initialize 
{ 
    [self fetchLocation]; 
}

+(void)fetchLocation
{
 mLocation = [[CLLocationManager alloc]init]; 
 mLocation.delegate = self;
 mLocation.desiredAccuracy = kCLLocationAccuracyBest;
 [mLocation startUpdatingLocation]; 
}

If I allocate the same class instance and add as delegate its crashing.

1 Answer 1

2

1) You can't call [self fetchLocation] from a static method like +initialize. That's because there's no self there (self refers to the current object, which is something you don't have in static methods.)

2) The line mLocation.delegate = self doesn't make sense for the same reason.

To fix your design, you can do one of the following things, depending on what makes the most sense in your app:

  • Make your class a singleton, and make that singleton a delegate for CLLocationManager
  • Put the location manager init code in the appropriate -init method of your class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.