发送浮点数到不兼容类型ID的参数 [英] Sending float to parameter of incompatible type id
问题描述
我正在创建一个按钮,该按钮使用核心数据保存点批注的名称,xCoordinate和yCoordinate.我可以成功保留名称,但是在尝试保存坐标时仍然出现此错误.我已经记录了正确的数据,但似乎无法保存.
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.
当我尝试为newPOI设置setValue时,出现以下错误: 向不兼容类型"id"的参数发送"float".
When I try to setValue for the newPOI, I get an error that reads: Sending 'float' to parameter of incompatible type 'id'.
在数据模型中,该属性设置为float. self.latitude和self.longitude的类型为float.
In the data model, the attribute is set to float. self.latitude and self.longitude are of type float.
我的方法有点粗糙,因为我对此还比较陌生,但是如果您能提供有关此错误的任何反馈,我将不胜感激.下面是我的方法代码.我不知道"id"在这里起作用.
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);
}
}
推荐答案
Core Data中的NSManagedObject属性必须是一个对象,而不是原始类型.在这种情况下,我们的yCoordinate是float类型.为了使用float类型的setValue :,必须首先将值包装在NSNumber中.
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"];
这篇关于发送浮点数到不兼容类型ID的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!