user1688346
user1688346

Reputation: 1970

NSManageObjectContext Save - unrecognized selector

Trying to save a new Cart Item and I am getting this error.

[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]);
}

Upvotes: 0

Views: 93

Answers (1)

Akhilrajtr
Akhilrajtr

Reputation: 5182

Try this,

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]);
}

Upvotes: 1

Related Questions