0

I am using CoreData and I have an NSManagedObject of type Thing. When I set a breakpoint I can console "po aThing" and it shows the data stored.

{
    thingID = 5181c56063ab02c4d1000016;
    distance = "2.075121";
    lat = "37.815834";
    lng = "-122.406417";
}

But, if I try to access the lat or lng (which are doubles in Core Data). I get "nan" returned. If I try "po aThing.lat" the console shows nan.

If I try "po aThing.thingID" the console shows 5181c56063ab02c4d1000016.

If I try to assign like newThing.lat = aThing.lat then newThing.lat becomes nan.

I am guessing it has to do with the data be a double?

UPDATE:

Here is how my .h had properties

@property (nonatomic) double lat;
@property (nonatomic) double lng;

Here is how I was using them

coordinate.latitude = thing.lat;
coordinate.longitude = thing.lng;

Here is how I changed them

@property (nonatomic) double *lat;
@property (nonatomic) double *lng;

And how I use them now and it seems to work

coordinate.latitude = *(thing.lat);
coordinate.longitude = *(thing.lng);
jdog
  • 10,351
  • 29
  • 90
  • 165
  • 3
    Core Data doesn't store numbers as integers or doubles, etc. They are stored within an `NSNumber` object. Either way it should still be printing out the value instead of returning it as `nan` but you can try with `po [aThing.lat doubleValue]` instead. – iwasrobbed May 02 '13 at 18:32
  • Actually that's not true. Core Data can use scalar properties for primitive data types. While generating a class, there's an option for exactly that - "Use scalar properties for primitive data types". – Anurag May 02 '13 at 19:51
  • Actually, that only affects how the subclass is generated, not how Core Data stores the data. – Tom Harrington May 02 '13 at 20:41
  • How Core Data stores the data is an implementation detail. It provides a default implementation for the setters and getters. An end user of an NSManagedObject subclass doesn't really have to care, and neither should they care about how Core Data does things under the hood. To an end user, there's a scalar property which they can set and get using the accessors provided by Core Data. The important thing to take away here is that the accessors around scalars deal with scalars, and not a wrapper like NSNumber. – Anurag May 02 '13 at 20:55

1 Answers1

0

As said in the comments above, switch from "double" to "NSNumber", and convert double to NSNumber like [NSNumber numberWithDouble:someDouble]

OR.... another (and probably a better) approach (as found in the apple documentation) is that you can keep "lat" as double, but under the hood, you make your own getter/setter and convert the double to NSNumber.

some.h
@property long lat;

some.m
@interface MyManagedObject (PrimitiveAccessors)
@property (nonatomic) NSNumber primitiveLat;
@end


- (long)lat {

    [self willAccessValueForKey:@"lat"];
    NSNumber *tmpValue = [self primitiveLat];
    [self didAccessValueForKey:@"lat"];
    return (tmpValue!=nil) ? [tmpValue longValue] : 0.0; // Or a suitable representation for nil.
}

- (void)setLat:(long)value {

    NSNumber *temp = @(value);
    [self willChangeValueForKey:@"lat"];
    [self setPrimitiveLat:temp];
    [self didChangeValueForKey:@"lat"];
}
Cam
  • 128
  • 5
  • The code above doesn't work. For instance, my lat going is 39.34343... and coming out using this code above as 7.87634... – jdog May 02 '13 at 21:28
  • Ah, derp. You need to use a long. – Cam May 13 '13 at 17:27
  • And if long doesn't work, swap out "long" for "long long". `-(long long)lat` `-(void)setLat:(long long)value` `[tmpValue longLongValue]` `long long lat`... Disclaimer: this is untested and that's why I'm not sure ATM if you need long or "long long". – Cam May 13 '13 at 17:28