Understanding an iOS declare return value

I’ve been playing around with CLLocationManager (the base class for Location updates).

I hooked up with its delegate to receive heading updates.

My app is correctly receiving CLHeading objects as pointers (Ptr).
The problem was reading the magneticHeading value.

The following declare always returns 0:

Declare function magneticHeading_ lib "CoreLocation" selector "magneticHeading" (obj as ptr) as Double

Return magneticHeading_(ptrHeading).value //Always returns 0.0

It took me a few hours to figure that the above declare should instead return a Structure that only contains one double value.
So I defined the following structure:

Structure CLHeadingStructure
  value as Double
End Structure

And then got the value like this:

Declare function magneticHeading_ lib "CoreLocation" selector "magneticHeading" (obj as ptr) as CLHeadingStructure

Return magneticHeading_(ptrHeading).value //Returns the correct value

@Greg_O or @Sam_Rowlands would you be able to explain why a Structure is needed here?

It’s not a structure, it’s a type alias to double. So a double should just work here.

Declare function magneticHeading_ lib "CoreLocation" selector "magneticHeading" (obj as ptr) as Double

Return magneticHeading_(ptrHeading)

CLLocation_magneticHeading is a better name for the function IMHO as you include the class name in the call, which makes it easier for you and others to look up in the future.

What is the .Value doing here? If it assumes a Ptr or Integer that’s a Ptr, then what you’ve done makes sense.

Made a copy-paste error at 3AM…

It should read:

Declare function magneticHeading_ lib "CoreLocation" selector "magneticHeading" (obj as ptr) as Double

Return magneticHeading_(ptrHeading) //Always returns 0.0

I completely agree with you. It is a type alias to double, but double does not work here.

I can share an example project if you or someone else is interested. Note: it can only be tested on a real device.

If a structure works and a Double does not, I believe the method is returning a pointer to the double.

1 Like

That is also my conclusion.
It’s not uncommon for Apple’s documentation to be not quite right, have you checked the header files for the class? Sometimes these can fill in the missing parts from the web documentation and sometimes not.

I would also check with multiple versions of iOS; as again it’s not uncommon for API to return different results for different versions, and Apple’s docs make no mention of this.

As for running an a physical device, that’s something I canna help you with here as I quit iOS last year.