NSManageObjectContext保存 - 无法识别的选择器

时间:2014-02-25 04:33:32

标签: ios core-data

尝试保存新的购物车项目,我收到此错误。

[CartItem save:]: unrecognized selector sent to instance 0xdd307f0
2014-02-25 12:24:45.206 Fashbowl[975:a0b] *** Terminating app due to uncaught exception        'NSInvalidArgumentException', reason: '-[CartItem save:]: unrecognized selector sent to    instance 0xdd307f0'




spAppDelegate *appDelegate = (spAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *moc=[appDelegate managedObjectContext ];
moc = [[NSManagedObjectContext alloc] init] ;
[moc setPersistentStoreCoordinator:[appDelegate persistentStoreCoordinator ]];
NSManagedObjectContext *itemMo =[CartItem insertInManagedObjectContext:moc];
NSError *error;
NSString *cartId=[NSString getUniqueId]; //new cart

[itemMo setValue:cartId forKey:@"cart_id"];
//  [itemMo setValue:[NSNumber numberWithDouble:[self.lblPrice.text doubleValue]] forKey:@"price"];
//  [itemMo setValue:[NSNumber numberWithDouble:[self.lblComparePrice.text doubleValue]] forKey:@"compare_at_price"];


if (![itemMo save:&error]) {
  NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}

1 个答案:

答案 0 :(得分:1)

试试这个,

spAppDelegate *appDelegate = (spAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *moc=[appDelegate managedObjectContext ];
if (!moc) {
     moc = [[NSManagedObjectContext alloc] init] ;
    [moc setPersistentStoreCoordinator:[appDelegate persistentStoreCoordinator ]];
}

CartItem *itemMo =[CartItem insertInManagedObjectContext:moc];
NSError *error;
NSString *cartId=[NSString getUniqueId]; //new cart

[itemMo setValue:cartId forKey:@"cart_id"];

if (![moc save:&error]) {
      NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}