views:

587

answers:

2

I have an NSLog statement in my -didUpdateToLocation method which prints the latitude of my location as expected:

NSLog(@"%g", newLocation.coordinate.latitude);

prints 37.3317

In the same method, i assign newLocation to a CLLocation instance variable for use elsewhere.

self.selectedLocation = newLocation;

But when i NSLog this using %g, i get this value -1.96638e-202

NSLog(@"%g", self.selectedLocation.coordinate.latitude);

What i have done wrong?

Thanks

A: 

in the .h file, what do you have for the property?

That is, you have a line like:

@property (copy)CLLocation* newLocation;

what is in the parenthesis?

justin
the property is (nonatomic, retain)
joec
Well, so much for my one guess. It doesn't seem likely, but since didUpdateLocation is asynchronos, something odd might be happening. What if you change the assignment to select.selectedLocation = [ [CLLocation alloc] initWithLatitude: newLocation.coordinate.latitude longitude: newLocation.coordinate.longitude] I can't imagine why you'd have to, though
justin
added a release to get rid of the potential leak
justin
add the release where? thanks
joec
Well thats even stranger, i did as you suggested using `[[alloc]init...]`, when i print `newLocation.coordinate.latitude` in `didUpdateToLocation`, it works, then i do the assignment, then print `selectedLocation.coordinate.latitude`, and get another weird number `2.42797e-288`, which is different to the first weird number. Whats going on here? Is there a special format code needed for latitudes/longitudes? Thanks.
joec
Oh, sorry, I screwed up my comment. Before doing the new alloc, add a release ( [self.selectedLocation release] ) just in case you have an existing self.selectedLocation.To be honest, I had to look up the %g print specifier, so I can assure you that you have the correct one. Also, it works fine in your first example. Wait, so you do the assignment using [[alloc] init...] and you get a new weird number? Out of curiousity, and because I'm out of ideas, what happens if you do the assignment before printing anything out?
justin
Ok, sorry, i messed up too - when i print after the assignment, the value is correct, 37.3317. So its back to square one, printing self.selectedLocation.coordinate.latitude outside of the didUpdateToLocation method gives a weird number. I'm new to debugging in Xcode, so i'm a bit lost - thanks for your help so far.
joec
Yeah, XCode is a little user-hostile at first. It gets easier. Anyway, just to confirm:1. print newLocation - works2. assign to instance variable using [[alloc] init] works in didUpdateToLocation ?3. print instance variable outside of didUpdateToLocation -- doesn't work? Am I understanding the sequence correctly? Or are you printing the value of self.selectedLocation inside of didUpdateToLocation and it is showing the incorrect number?
justin
No youre correct. That's the sequence and inside of didUpdateToLocation the instance variable is fine. Outside it prints incorrectly. The incorrect print appears on the console before the prints in didUpdateToLocation even though I am printing after calling [startUpdatingLocation]
joec
Okay, you are sure that the assignment has happened before you print the instance variable, right? There can be a considerable lag after calling startUpdatingLocation before didUpdateToLocation gets called. Can you confirm that your NSLogs appear in this order:1. display newLocation2. Assignment of newLocation to self.selectedLocation.3. display self.selectedLocationIf they do, try adding retain to the end of the [ [ alloc ] init...]
justin
joec
The only difference in the simulator and the device is that the simulator will always return Apples address.Yes, there is a substantial lag between sending the startUpdatingLocation and the didUpdateToLocation (and it's greater on the device); the GPS has to start, and I think I've seen 10 seconds from a cold start until the first didUpdateToLocation (depending on all sorts of things, including location of wireless routers that can be used to determine the location, position of satellites, solar flares). The weird number you are seeing is normal; it means selectedLocation is uninitialized.
justin
Ok Thanks, but how then can I get a pause until I have a location update? Because after startUpdatingLocation (in viewDidLoad) I have a call to a webservice which is getting passed with the weird number. Obviously this is failing. Thanks.
joec
Put a semaphor before the call to the website. Have the mutex changed inside of didUpdateToLocation. Block on the semaphor before calling the web site.I have some sample code for this if you want it
justin
Yes please that would be very helpful. What's a semaphore and mutex?
joec
A semaphor is a flag that prevents 2 threads from accessing the same bit of data at the same time. In ObjectiveC, the class is an NSLock class.Basically, you want to this:startUpdatingLocation, then waitUntilIhaveAValidLocation, then getWebPageWithThisLocation. The object that would provide the waituntilIhaveavalidlocation functionality is a semaphor. Unfortunately my code doesn't do what I thought it does. Give me a little bit and I'll dig something up.
justin
A: 

Hi,

Are you testing this in the simulator? If you are it will always return the latitude as "37.33" and longitude as "-122.0" or near around which is the location of Cupertino, California, USA(The apple headquarters).

So if this is not the case show us some your code snippet.

Hope I helped you.

Thanks,

Madhup

Madhup
I know the sim always returns cupertino - the issue is the negative value to the e power i'm seeing when printing out the instance variable. thanks.
joec