Sending a float to an incompatible type id parameter
I'm in the middle of creating a button that uses kernel data to store the name, xCoordinate and yCoordinate of the dot annotation. I can save the name successfully, but I keep getting this error when I try to save the coordinate. I wrote down the correct data, I just can't save it.
When I try to setValue for newPOI, I get the error: Sending 'float' to parameter of incompatible type 'id'.
In the data model, the attribute is float. self.latitude and self.longitude are float types.
My method is a bit rough because I'm relatively new to this, but I would appreciate any feedback you can give me about the bug. Below is my code for the method. I don't understand where "id" is playing 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);
}
}
source to share
The NSManagedObject attributes in Core Data must be an object, not a primitive type. In this case, our yCoordinate was a float. To setValue: of type float, you must first cast the value to NSNumber.
[newPoi setValue:[NSNumber numberWithFloat:someFloat] forKey:@"yCoordinate"];
against.
[newPoi setValue:someFloat forKey:@"yCoordinate"];
source to share