I'm in the middle of creating a button that uses core data to save a name, xCoordinate, and yCoordinate of a point annotation. I can successfully persist the name, but I keep getting this error when I try to save a coordinate. I've logged the correct data, I just can't seem to save it.
When I try to setValue for the newPOI, I get an error that reads: Sending 'float' to parameter of incompatible type 'id'.
In the data model, the attribute is set to float. self.latitude and self.longitude are of type float.
My method is a little rough because I'm relatively new to this, but I would appreciate any feed back you could give me concerning the error. Below is my code for the method. I don't understand where 'id' comes into play here.
-(void) saveSelectedPoiName:(NSString *)name withY:(float)yCoordinate withX:(float)xCoordinate{
self.pointFromMapView = [[MKPointAnnotation alloc] init];
self.annotationTitleFromMapView = [[NSString alloc] init];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
self.annotationTitleFromMapView = name;
self.latitude = yCoordinate;
self.longitude = xCoordinate;
NSEntityDescription *entityPOI = [NSEntityDescription entityForName:@"POI" inManagedObjectContext:context];
NSManagedObject *newPoi = [[NSManagedObject alloc] initWithEntity:entityPOI insertIntoManagedObjectContext:context];
//create new POI record
[newPoi setValue:name forKey:@"name"];
[newPoi setValue:yCoordinate forKey:@"yCoordinate"]; <---error happens here for yCoordinate.
NSError *saveError = nil;
if (![newPoi.managedObjectContext save:&saveError]) {
NSLog(@"Unable to save managed object");
NSLog(@"%@, %@", saveError, saveError.localizedDescription);
}
}
NSManagedObject attributes in Core Data must be an object and not of a primitive type. In this case our yCoordinate was of type float. In order to setValue: of a float type you must first wrap the value in an NSNumber.
[newPoi setValue:[NSNumber numberWithFloat:someFloat] forKey:@"yCoordinate"];
vs.
[newPoi setValue:someFloat forKey:@"yCoordinate"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With