views:

341

answers:

2

I have a custom class that has as an instance variable of a coordinate:

CLLocationCoordinate2D eventLocation;
@property(nonatomic) CLLocationCoordinate2D eventLocation;

I'm parsing an xml file that has an optional field that may or may not be there. If it is i set it like so:

CLLocationCoordinate2D location;
NSArray *coordinateArray = [paramValue componentsSeparatedByString:@","];
if ([coordinateArray count] >= 2) {
    location.latitude = [[coordinateArray objectAtIndex:0] doubleValue];
    location.longitude = [[coordinateArray objectAtIndex:1] doubleValue];
} else {
    NSLog(@"Coordinate problem");
}
info.eventLocation = location;

What I do this is basically add an annotation on a map

annotation.coordinate = alert.info.eventLocation;

I know I need to do some checking here to make sure that that exists but I'm not allowed to do a if (info.eventLocation == nil) or even if (info.eventLocation.latitude == nil)

This seems like a very basic question but I've done some searching and no one's been able to really provide a good answer/idea. Is my architecture completely off?

+8  A: 

Because CLLocationCoordinate2D is a struct, there's not such thing as a nil value. Objective-C will initialize structs to 0 if they are object instance variables, so if you don't set a value for eventLocation, annotation.coordinate.logintude and eventLocation.lattitude will both be 0. Since this is a valid location, it's not a useful marker.

I would define a non-phyical value:

static const CLLocationDegrees EmptyLocation = -1000.0;
static const CLLocationCoordinate2D EmptyLocationCoordinate = {EmptyLocation, EmptyLocation}

and then assign this value to your alert.info.eventLocation = EmptyLocationCoordinate to represent an empty value. You can then check if (alert.info.eventLocation == EmptyLocationCoordinate).

Barry Wark
When i add this to my .h file of my custom object I'm getting errors: Initializer element is not constant(near initialization for 'EmptyLocationCoordinate.latitude') Initializer element is not constant(near initialization for 'EmptyLocationCoordinate.longitude')
aleclerc
Actually, the alert.info.eventLocation = EmptyLocationCoordinatestill gives a "Invalid operands to binary !=" Error
aleclerc
Ah, perhaps you need to #define EmptyLocation.
Barry Wark
It's not quite accurate to say Objective-C will initialize structs to 0. It will zero out instance variables, but structs created on the stack are treated just like any other auto variable.
Chuck
@aleclerc: That assignment doesn't involve any != operator, so I think your error is elsewhere.
Chuck
@chuck sorry I was using the comparison with both != and == in different places
aleclerc
+2  A: 

I used the code above except it wouldn't let me declare a const with another const, so i simply changed it to:

static const CLLocationDegrees EmptyLocation = -1000.0;
static const CLLocationCoordinate2D EmptyLocationCoordinate = {-1000.0, -1000.0};

I also added in my init for the class:

eventLocation = EmptyLocationCoordinate;

Thanks for the help Barry.

aleclerc