[图片数量]: 未识别的选择器发送到实例

5
当保存managedObjectContext时,我收到以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[Image count]: unrecognized selector sent to instance

代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo {

// Delete any existing image.
Image *oldImage = _place.image;
if (oldImage != nil) {
    [_place.managedObjectContext deleteObject:oldImage];
}

Image *image = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:_place.managedObjectContext];
image.imageData = UIImageJPEGRepresentation(selectedImage, 0.5);
_place.image = image;
[_place.managedObjectContext save:nil]; //Here is the crash
[self dismissViewControllerAnimated:YES completion:nil];

Image实体有两个一对一关系:placetrip

@interface Image : NSManagedObject

@property (nonatomic, retain) NSData * imageData;
@property (nonatomic, retain) Trip *trip;
@property (nonatomic, retain) Place *place;

@end

无法理解为什么在一对一关系中出现[图像计数]错误。

更新:

@interface Place : NSManagedObject

@property (nonatomic, retain) NSString * address;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) id thumbnail;

@property (nonatomic, retain) Image *image;

@end

Upd.2:

*** First throw call stack:
(
    0   CoreFoundation                      0x022935e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01cef8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x02330903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0228390b ___forwarding___ + 1019
    4   CoreFoundation                      0x022834ee _CF_forwarding_prep_0 + 14
    5   CoreData                            0x00803df8 -[NSSQLCore _knownOrderKeyForObject:from:inverseToMany:] + 200
    6   CoreData                            0x0073ad50 -[NSSQLCore _populateRowForOp:withObject:] + 1120
    7   CoreData                            0x00750e27 -[NSSQLCore recordValuesForInsertedObject:] + 71
    8   CoreData                            0x00739b5d -[NSSQLCore recordChangesInContext:] + 685
    9   CoreData                            0x00738925 -[NSSQLCore saveChanges:] + 565
    10  CoreData                            0x0070555c -[NSSQLCore executeRequest:withContext:error:] + 412
    11  CoreData                            0x00705050 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 4704
    12  CoreData                            0x00731ccc -[NSManagedObjectContext save:] + 764
    13  Travel Plan                         0x0001cf12 -[PlaceViewController imagePickerController:didFinishPickingImage:editingInfo:] + 738
    14  UIKit                               0x00cd5e6c -[UIImagePickerController _imagePickerDidCompleteWithInfo:] + 488
    15  PhotoLibrary                        0x13694e94 PLNotifyImagePickerOfImageAvailability + 106
    16  PhotosUI                            0x14411585 -[PUUIPhotosAlbumViewController handleNavigateToAsset:inContainer:] + 401
    17  PhotosUI                            0x143947b4 -[PUPhotosGridViewController collectionView:shouldSelectItemAtIndexPath:] + 577
    18  UIKit                               0x0106dc0b -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 173
    19  UIKit                               0x010861f8 -[UICollectionView _userSelectItemAtIndexPath:] + 189
    20  UIKit                               0x010863b5 -[UICollectionView touchesEnded:withEvent:] + 437
    21  libobjc.A.dylib                     0x01d01874 -[NSObject performSelector:withObject:withObject:] + 77
    22  UIKit                               0x00bb6902 forwardTouchMethod + 271
    23  UIKit                               0x00bb6972 -[UIResponder touchesEnded:withEvent:] + 30
    24  libobjc.A.dylib                     0x01d01874 -[NSObject performSelector:withObject:withObject:] + 77
    25  UIKit                               0x00bb6902 forwardTouchMethod + 271
    26  UIKit                               0x00bb6972 -[UIResponder touchesEnded:withEvent:] + 30
    27  libobjc.A.dylib                     0x01d01874 -[NSObject performSelector:withObject:withObject:] + 77
    28  UIKit                               0x00bb6902 forwardTouchMethod + 271
    29  UIKit                               0x00bb6972 -[UIResponder touchesEnded:withEvent:] + 30
    30  UIKit                               0x00dd1c7f _UIGestureRecognizerUpdate + 7166
    31  UIKit                               0x00a9c19a -[UIWindow _sendGesturesForEvent:] + 1291
    32  UIKit                               0x00a9d0ba -[UIWindow sendEvent:] + 1030
    33  UIKit                               0x00a70e86 -[UIApplication sendEvent:] + 242
    34  UIKit                               0x00a5b18f _UIApplicationHandleEventQueue + 11421
    35  CoreFoundation                      0x0221c83f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    36  CoreFoundation                      0x0221c1cb __CFRunLoopDoSources0 + 235
    37  CoreFoundation                      0x0223929e __CFRunLoopRun + 910
    38  CoreFoundation                      0x02238ac3 CFRunLoopRunSpecific + 467
    39  CoreFoundation                      0x022388db CFRunLoopRunInMode + 123
    40  GraphicsServices                    0x046299e2 GSEventRunModal + 192
    41  GraphicsServices                    0x04629809 GSEventRun + 104
    42  UIKit                               0x00a5dd3b UIApplicationMain + 1225
    43  Travel Plan                         0x0004c53d main + 141
    44  libdyld.dylib                       0x02dd170d start + 1
)

1
请同时显示place的定义。抱歉,您没有提供原始文本。请提供原始文本以便我可以为您进行翻译。 - Daij-Djan
@Daij-Djan 我现在就会添加它。 - Shmidt
如果您在异常上设置断点,那么最终到达[Image count]的跟踪会是什么样子? - Phillip Mills
2
那个 inverseToMany: 看起来确实有点可疑。有没有可能之前在代码中存在一个一对多的关系,但现在已经从代码中删除了,但在模型中仍然存在?或者某些缓存的模型部分还没有被清除?(我知道这很难说,但我建议尝试完全“清理”和“删除派生数据”,看看是否有什么不同步的地方。) - Phillip Mills
1
以及MOM中相关实体的定义?看起来CD认为它应该是一组图像或其他东西。 - Daij-Djan
显示剩余4条评论
2个回答

20

在更改模型中的某些关系后,我遇到了完全相同的问题。这似乎是Xcode中的一个错误,当您将一个“to many:ordered”关系更改为“to one”关系时,它会出现。就像Xcode仍然认为关系是多个,因为“有序”复选框已被选中。

解决此问题的一种非常简单的方法是将其更改回“to many”关系,取消选择“ordered”,然后再将其更改回“to one”关系。


你真是个救星,哈哈:) 我已经检查了好久了。 - Jay Mayu
这个错误仍然存在,而这个解决方案仍然有效。 - aaronium112

8

这是Xcode中的一个错误。将关系设置为多个,然后再设置为一个时,它开始正常工作,不会出现错误。


1
我也遇到了这样的问题。通过从模型中删除“损坏”的实体(包括所有关系),构建项目,然后从头开始创建它来解决了这个问题。 - Artem Shmatkov
Médéric Petit的答案比zakhej的快得多且更有效。我建议先尝试他的方法,它对我非常有效。 - Zoltán
@pallzoltan 他写的跟我一模一样,几乎晚了将近6个月。 - Shmidt
@Shmidt提到了取消有序复选框的选择,这是关键所在。我按照你的建议去做了,但直到我取消了有序复选框的选择才成功 ;) - Zoltán

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接