我正在创建一个按钮,使用核心数据来保存点注释的名称,xCoordinate和yCoordinate.我可以成功保留名称,但是当我尝试保存坐标时,我不断收到此错误.我已经记录了正确的数据,我似乎无法保存它.
当我尝试为newPOI设置setValue时,我收到一条错误,内容如下:将'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"); …Run Code Online (Sandbox Code Playgroud) 我从一个声誉良好的教程中提取了这段代码,当我运行应用程序时,我在CGFloat实例*imageHeight上收到NaN错误.但是,如果我将类型更改为NSUInteger,则应用程序构建得很好.是否有人可以解释发生的事情.我理解NaN = Not A Number,但CGFloat应该可以正常工作作为数据类型.
CGFloat imageHeight = self.mediaItem.image.size.height / self.mediaItem.image.size.width * CGRectGetWidth(self.contentView.bounds);
self.mediaImageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.contentView.bounds), imageHeight);<--NaN ERROR is thrown here
Run Code Online (Sandbox Code Playgroud)
NSUInteger imageHeight = self.mediaItem.image.size.height / self.mediaItem.image.size.width * CGRectGetWidth(self.contentView.bounds);
self.mediaImageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.contentView.bounds), imageHeight);
Run Code Online (Sandbox Code Playgroud)
- (void) layoutSubviews {
[super layoutSubviews];
NSUInteger imageHeight = self.mediaItem.image.size.height / self.mediaItem.image.size.width * CGRectGetWidth(self.contentView.bounds);
self.mediaImageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.contentView.bounds), imageHeight);
CGSize sizeOfUsernameAndCaptionLabel = [self sizeOfString:self.usernameAndCaptionLabel.attributedText];
self.usernameAndCaptionLabel.frame = CGRectMake(0, CGRectGetMaxY(self.mediaImageView.frame), CGRectGetWidth(self.contentView.bounds), sizeOfUsernameAndCaptionLabel.height);
CGSize sizeOfCommentLabel = [self sizeOfString:self.commentLabel.attributedText];
self.commentLabel.frame = CGRectMake(0, CGRectGetMaxY(self.usernameAndCaptionLabel.frame), …Run Code Online (Sandbox Code Playgroud)