【发布时间】:2015-10-07 20:23:44
【问题描述】:
我正在创建一个按钮,该按钮使用核心数据来保存点注释的名称、xCoordinate 和 yCoordinate。我可以成功保留该名称,但是当我尝试保存坐标时,我不断收到此错误。我已经记录了正确的数据,但我似乎无法保存它。
当我尝试为 newPOI 设置值时,我收到一条错误消息: 将“float”发送到不兼容类型“id”的参数。
在数据模型中,属性设置为浮动。 self.latitude 和 self.longitude 是 float 类型。
我的方法有点粗糙,因为我对此比较陌生,但如果您能就该错误向我提供任何反馈,我将不胜感激。下面是我的方法代码。我不明白“id”在哪里发挥作用。
-(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);
}
}
【问题讨论】:
-
一般来说,一条表明它正在寻找 id 类型参数的消息是在寻找参考/对象。
-
你知道为什么会出现这个错误吗?具体来说?
-
就像我提到的,如果我猜是因为你没有传递一个对象 (setValue:)。
-
我现在明白了。 Float 是原始类型,NSManagedObject 属性需要是对象。我需要将值包装在我假设的 NSNumber 中。
标签: ios methods parameters compiler-errors