Reputation: 113757
With objects, I can write an accessor to return nil
if the object isn't valid:
- (Weather *)howsTheWeather
{
if (![WeatherNetwork isAvailable]) {
return nil;
}
Weather *weatherNow = [WeatherNetwork getWeather];
return weatherNow;
}
I'd like to do this with a CLLocationCoordinate2D
, a struct
for holding a latitude and longitude. I tried this:
- (CLLocationCoordinate2D)coordinate
{
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(self.latitude, self.longitude);
if (!CLLocationCoordinate2DIsValid(coord)) {
return nil;
}
return coord;
}
But the compiler complains "Returning 'void *' from a function with incompatible result type 'CLLocationCoordinate2D'"
What's the best way to deal with this kind of situation? I was thinking of writing a separate method -(BOOL)isLocationValid:(CLLocationCoordinate2D)coordinate
. Is there a better way to deal with this?
Upvotes: 0
Views: 385
Reputation: 80603
A struct cannot be null. You will need to go with option 2 (checking the validity of the struct based on the data it contains). Alternatively you could wrap your struct in an pseudo-object that is capable of being nil'ed, but that seems alot of work for spurious benefit.
Upvotes: 1