sunny
sunny

Reputation: 3891

inserting CLLocation latitude from coordinate into NSMutable Array

I'm doing something stupid, but not sure what. Here's my code:

NSMutableArray *latitudes = [[NSMutableArray alloc] init];


NSMutableArray *locationArray = [NSMutableArray array];
for (CLLocation *location in self.allPins) {
    Location *locationObject = [NSEntityDescription insertNewObjectForEntityForName:@"Location"
                                                             inManagedObjectContext:self.managedObjectContext];

    locationObject.timestamp = location.timestamp;
    locationObject.latitude = [NSNumber numberWithDouble:location.coordinate.latitude];
    locationObject.longitude = [NSNumber numberWithDouble:location.coordinate.longitude];
    [locationArray addObject:locationObject];
    [latitudes addObject:[NSNumber numberWithFloat: location.coordinate.latitude]];
    NSLog(@"here is the array for latitudes count: %d", latitudes.count);
    NSLog(@"here is the value for first latitude: %d", latitudes[0]);
}

self.allPins holds all locations received from CLLocationManager. Once a user is done moving and wants to save, I run the snippet above to both load the trajectory into a CoreData class to save on the user's phone and also into arrays to upload the data online. I'd like to have an array of latitudes longitudes, etc, but this isn't working out. While I can insert the information into my CoreData object just fine, I can't seem to simply extract the doubles/floats (tried it both ways but I thought it shouldn't make a difference?) out of the CLLocation coordinate. When I run the following code, I get a count for the latitudes array, but I get nonsense for the value:

2015-05-26 15:25:22.934 MileRun[665:196255] here is the value for first latitude: 426649664

Upvotes: 1

Views: 307

Answers (1)

timgcarlson
timgcarlson

Reputation: 3146

You are not printing the value of the NSNumber, but what I believe is the address of the NSNumber (somebody might correct me here if that is not was 426649664 is in his log).

Change NSLog(@"here is the value for first latitude: %d", latitudes[0]); to

NSLog(@"here is the value for first latitude: %f", latitudes[0].floatValue);

NSLog(@"here is the value for first latitude: %@", latitudes[0]);

MORE INFO EDIT: The format specifier %d is for integers. You could print out the NSNumber with the format specifier for objects, which is %@. This would allow you to log with NSLog(@"number = %@", latitudes[0]) and get the value for the NSNumber that way.

Also, when in doubt, set a breakpoint in your code and inspect the objects in the array with the debugger.

Upvotes: 1

Related Questions