Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending float to parameter of incompatible type id

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);
    }
}
like image 755
Alex Blair Avatar asked Dec 02 '22 16:12

Alex Blair


1 Answers

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"];
like image 179
Alex Blair Avatar answered Dec 23 '22 04:12

Alex Blair