Reputation: 11297
I am allowing the user to select a location and want to test for it in another screen:
if (myManager.centerOfMap == (id)[NSNull null]) {
self.centerOfMap = myManager.centerOfMap;
}
However I know the above is not right as it gives me a pointer conversion warning.
I then tried to do:
if (CLLocationCoordinate2DIsValid(*(myManager.centerOfMap))) {
self.centerOfMap = myManager.centerOfMap;
}
However my program crashes due to to EXC_BAD_ACCESS since myManager.centerOfMap is not set the first time. How do I make it so that it lets me know if a CLLocationCoordinate2D has been assigned a value or not?
Upvotes: 1
Views: 1039
Reputation: 924
Note that NSNull
is actually a (singleton) object. The idea is to use it when you want to have a null value object (ie. where you can't just use a nil value or pointer). For example, as an object in an NSDictionary
. It is not what you're looking for here.
If your centerOfMap
is created as a CLLocationCoordinate2D
this is just a simple structure, not an object. Note you would create it like this:
CLLocationCoordinate2D centerOfMap;
Not like this:
CLLocationCoordinate2D *centerOfMap;
The CLLocationCoordinate2D structure is defined as this:
typedef struct {
CLLocationDegrees latitude;
CLLocationDegrees longitude;
} CLLocationCoordinate2D;
So, if you want to know if a given value in this is good, you need to do one of the following:
1) Have a separate member variable that says whether a valid location has been added. For example, BOOL retrievedCenterOfMap
. Then set this to YES when you configure centerOfMap, so then your test becomes:
if ([myManager retrievedCenterOfMap]) {
self.centerOfMap = myManager.centerOfMap;
}
2) Set known invalid values in your centerOfMap
variable. CLLocationCoordinate2DIsValid()
will tell you if the centerOfMap
value is valid in terms of whether it is a valid coordinate or not, but you should differentiate that from whether you have actually set it or not! The question then becomes what invalid value should you use, and will anyone checking your code in the future know what you did here.
Upvotes: 2
Reputation: 5589
You didn't say but I assume since you are passing it to CLLocationCoordinate2DIsValid
that myManager.centerOfMap
is of type (CLLocationCoordinate2D *)
. CLLocationCoordinate2D
is a struct not an Objective-C object. If myManager.centerOfMap
is really a pointer to CLLocationCoordinate2D
you will first need to check it is not NULL and then check that it is valid:
if (myManager.centerOfMap && CLLocationCoordinate2DIsValid(*(myManager.centerOfMap))) {
self.centerOfMap = myManager.centerOfMap;
}
You may find it easier, however, to just store CLLocationCoordinate2D directly rather than pointers to them in your instance variables.
Upvotes: 0
Reputation: 8502
Have you tried:
if (CLLocationCoordinate2DIsValid(myManager.centerOfMap)) {
self.centerOfMap = myManager.centerOfMap;
}
Also, in your myManager
class, you may want to make sure you set centerOfMap
to an invalid CLLocationCoordinate2D
value when the class initializes. That way you know for sure that it starts out with an invalid value.
Upvotes: 2