1

I've encountered an issue when storing a MKMapItem that I was able to resolve by looking at compiler warnings, but I don't understand why I was able to resolve it or if I utilized "best practices".

I have an Object model that stores latitude and longitude coordinates from an MKMapItem separately as doubles in an NSManagedObject. When I go to Editor\Create NSManagedObject Subclass and create the my class, the header looks like this:

@class LocationCategory;

@interface PointOfInterest : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * address;
// Xcode spat out NSNumber instead of the double specified in the model setup screen
@property (nonatomic, retain) NSNumber * latitude;
@property (nonatomic, retain) NSNumber * longitude;
@property (nonatomic, retain) NSString * note;
@property (nonatomic, retain) LocationCategory *locationCategory;

@end

All was well until I tried to add an object to my managedObjectContext I got these warnings:

Assigning to 'NSNumber *' from incompatible type 'CLLocationDegrees' (aka 'double')

on these lines:

newPOI.latitude = self.item.placemark.location.coordinate.latitude;
newPOI.longitude = self.item.placemark.location.coordinate.longitude;

I fixed it by changing my PointOfInterest : NSManagedObject subclass:

@property (nonatomic) double latitude;
@property (nonatomic) double longitude;

Is this the best way to make the compiler happy or is there a better way?

Adrian
  • 16,233
  • 18
  • 112
  • 180
  • 1
    Your solution is entirely reasonable and valid. Really, there's no need to change it or to worry that you're doing anything wrong, it's a good solution as-is. – Tom Harrington Mar 13 '15 at 20:50

1 Answers1

1

I would suggest you change the properties of your PointOfInterest subclass back to NSNumber, and then change your assignment of latitude and longitude as follows:

newPOI.latitude = [NSNumber numberWithDouble:self.item.placemark.location.coordinate.latitude];
newPOI.longitude = [NSNumber numberWithDouble:self.item.placemark.location.coordinate.longitude];

Then when you want to use the latitude:

self.item.placemark.location.coordinate.latitude = [newPOI.latitude doubleValue];

etc.

Steve
  • 1,840
  • 17
  • 20
  • Thank you so much! My "solution" felt a little forced/hackish. I'm glad I posted a question because I learned something. Thanks again. – Adrian Mar 13 '15 at 18:31
  • 1
    @AdrianB Glad I could help. Have fun with your project! – Steve Mar 13 '15 at 18:32
  • @Steve , can you clarify why you're suggesting to *not* use scalars? Scalars have been supported on managed object subclasses for quite some time. The author's "solution" used scalars and was entirely correct. – quellish Mar 16 '15 at 06:43
  • @quellish In the event that a location is not available, NSNumber will allow a nil setting -- 0 latitude and 0 longitude are a valid latitude/longitude pair and shouldn't be used to indicate that a location was not specified, whereas nil would indicate that the data has not been set. – Steve Mar 16 '15 at 14:52