-3

I have a location app that already successfully asks the user to enable location services, and then can show them their coordinates on a button press as well. So I decided to play around with everything that is available in the CLLocationManager reference provided by xcode.

I decided to setup a bool method called "locationServicesEnabled". It returns a value of YES(1) or NO(0). I declared the method and then went to implement it. I am trying to have NSLog print the bool result out to the console when you open the app.

Here is how I declared the BOOL method in my ViewController.m file:

@interface ViewController () <CLLocationManagerDelegate>


-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
+ (BOOL)locationServicesEnabled;

@end

And here is how I implemented the BOOL method in ViewController.m:

+ (BOOL)locationServicesEnabled{

[self locationServicesEnabled];

NSLog(@"%hhd", self.locationServicesEnabled);

return 0;

}
user3117509
  • 833
  • 3
  • 14
  • 32
  • The method `+ (BOOL)locationServicesEnabled` is calling itself. You need to have a variable to hold the boolean value. – CainaSouza Dec 23 '13 at 16:23
  • Thank you for the help. Are you saying I need to change it to something like this? +(BOOL)locationServicesEnabled = variableName or do I need to create an object like this +(BOOL)locationServicesEnabled * objectName = – user3117509 Dec 23 '13 at 16:28
  • none of the code you just added is valid objective-c code. As I mentioned in my answer your issue for the BOOL not printing is because of a recursive call to its self. If you have some mis understanding beyond this point please edit the question to provide additional details. – Simon McLoughlin Dec 23 '13 at 16:36
  • 2
    Voted to close because you have demonstrated no understanding of the problem. You clearly are still in the very early learning stages of Objective-C where you would benefit more from reading a good book on the subject than trying to learn blindly without any prior understanding. – Jasarien Dec 23 '13 at 16:40
  • Jasarien thank you for providing no value what so ever. Coding is the best way to learn coding. Period. – user3117509 Dec 23 '13 at 16:45

2 Answers2

1

Why do you want to create an extra method? You could minimize the risk of errors by using already available ones: NSLog(@"Location services enabled: %d",[CLLocationManager locationServicesEnabled]);

tilo
  • 14,009
  • 6
  • 68
  • 85
  • I would put the NSLog code you just shared inside the BOOL methods body correct? – user3117509 Dec 23 '13 at 16:36
  • You could do this, yes. But ideally you wouldn't create an extra method at all but just use this line of code. – tilo Dec 23 '13 at 16:37
  • Thank you so much! This worked and just made me realize what a dumb mistake I was making. I have no idea why I was even typing out the unneeded BOOL function when I could have just printed it out already. Please clarify though, the reason for this is because the CLLocationManager class and all of it's methods are readily available to me because I imported the CoreLocation.h file into my ViewController.m file correct? – user3117509 Dec 23 '13 at 16:44
0

I think your issue might be the fact that you are calling your function inside its self:

+ (BOOL)locationServicesEnabled
{

     [self locationServicesEnabled]; <- should this be here

     NSLog(@"%hhd", self.locationServicesEnabled);

     return 0;
}
Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56