我正在创建一个按钮,使用核心数据来保存点注释的名称、x坐标和y坐标。我可以成功地保留名称,但是当我尝试保存坐标时,我一直得到这个错误。我已经记录了正确的数据,只是似乎无法保存它。
当我尝试为新POI设置值时,我会收到一个错误,内容如下:将“float”发送到不兼容类型“id”的参数。
在数据模型中,属性被设置为float。self.latitude和self.longitude是float类型。
我的方法有点粗糙,因为我相对较新,在这方面还请您提供反馈。以下是我的方法代码。我不明白'id'在这里出现的原因。
当我尝试为新POI设置值时,我会收到一个错误,内容如下:将“float”发送到不兼容类型“id”的参数。
在数据模型中,属性被设置为float。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);
}
}