更新获取的对象导致'无法识别的选择器发送到实例'错误

时间:2010-07-13 02:08:40

标签: iphone objective-c core-data ipad

每当我尝试在对象上设置任何属性时,尝试更新已获取的对象并将“无法识别的选择器发送到实例”错误,这是我的代码:

NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *entities = [NSEntityDescription entityForName:@"PurchaseOrderItem" inManagedObjectContext:managedObjectContext];
[request setEntity:entities];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ItemSKU=%@",collectionItem.ItemSKU];
[request setPredicate:predicate];

NSArray *results= [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

    if ([results count]==0) {
     /* add another purchase order item */
    PurchaseOrderItem *newPOItem = (PurchaseOrderItem*) [NSEntityDescription insertNewObjectForEntityForName:@"PurchaseOrderItem" inManagedObjectContext:managedObjectContext];
    [newPOItem setProductName:productName.text];
    [newPOItem setPrice:price];
    [newPOItem setQuantity:qty];
    [newPOItem setItemSKU:ItemSKU];
    [newPOItem setSubTotal:itemSubTotal];   
        }else {
    /* update item */
    PurchaseOrderItem *updateObject = (PurchaseOrderItem*)[results objectAtIndex:0];
    [updateObject setSubTotal:itemSubTotal];
    [updateObject setQuantity:qty];
        }

编辑: 这是确切的错误:

2010-07-12 22:07:37.920 RCoreData[46733:207] *** -[PurchaseOrder setSubTotal:]: unrecognized selector sent to instance 0x587b270
2010-07-12 22:07:37.921 RCoreData[46733:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[PurchaseOrder setSubTotal:]: unrecognized selector sent to instance 0x587b270'
2010-07-12 22:07:37.921 RCoreData[46733:207] Stack: (
    46504016,
    47661868,
    46512731,
    45975158,
    45971954,
    209089,
    3382510,
    3879998,
    3889344,
    3884141,
    3509736,
    3401283,
    3432920,
    53940604,
    45783196,
    45779112,
    53934237,
    53934434,
    3425138,
    10940,
    10794
)
terminate called after throwing an instance of 'NSException'

注意:添加项目效果很好,更新部分是抛出错误:

/* update item */
  PurchaseOrderItem *updateObject = (PurchaseOrderItem*)[results objectAtIndex:0];
  [updateObject setSubTotal:itemSubTotal];
  [updateObject setQuantity:qty];

2 个答案:

答案 0 :(得分:1)

PurchaseOrderItem *newPOItem = (PurchaseOrderItem*) [NSEntityDescription insertNewObjectForEntityForName:@"PurchaseOrderItem" inManagedObjectContext:managedObjectContext];

我认为这条线有问题。你确定你的newPOItem在正确的类中,你确定该转换效果很好吗?您可以尝试使用以下代码:[newPOItem isKindOfClass:] and [newPOItem respondsToSelector:]

答案 1 :(得分:0)

首先,演员阵容完全没必要。 -insertNewObjectForEntityForName:inManagedObjectContext:-objectAtIndex:都返回id这是一个通用对象。演员阵容 nothing

其次,当你在调试器中运行它时,你得到什么

po 0x587b270

这将告诉您尝试将消息发送到的对象。

第三,您没有检查错误。在开始操作返回的数组之前,应始终检查错误。