开发者

Classic CoreData error

I'm getting this classic error :

The model used to open the store is incompatible with the one used to create the store

This is how it's implemented :

 NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 

 NSManagedObject *newShot = [NSEntityDescription insertNewObjectForEntityForName:@"shotName" inManagedObjectContext:context];

NSString *newName= @"test"; 
 [newShot setName:newName];

And this is how it's designed :

Classic CoreData error

Classic CoreData error

No only I'm getting a crash with the message above, I'm also getting this warning :

'NSManagedObject' may not respond to '-setName:'

Obviously something is wrong somewhere, I think I'm using Strings on both side开发者_Python百科 though .


Edit, I'm now using this after Eimantas's comment :

 NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
 NSManagedObject *newShot = [NSEntityDescription insertNewObjectForEntityForName:@"shotName" inManagedObjectContext:context];
 NSString *newName= @"test"; 
 [newShot setValue:newName forKey:@"shotNumber"]; 
 [context saveAction];

But I`m still getting :

'NSManagedObjectContext' may not respond to '-saveAction'


Use setValue:forKey:

UPDATE

NSManagedObjectContext has save method, not saveAction. So:

NSError *error = nil;
[context save:&error]
if (error) {
  [NSApp presentError:error];
  return;
}


insertNewObjectForEntityForName:@"shotName" must be insertNewObjectForEntityForName:@"Shots". Shots is the entity name. shotName is the name of an attribute of the entity Shots. Also, like with Objective-C class names, it's standard to use singular names for your entity objects. So, Shots should be be Shot (recommended, but not required).

Also, if you change around your AppName.xcdatamodel file & generate new NSManagedObject files, you may also get the error: The model used to open the store is incompatible with the one used to create the store upon app launch. It's because it's using the old persistent store file. I call it: AppName.sqlite, but you may have a different name for this file. Search in your project for something like:

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"AppName.sqlite"]];

Then, once you know the name, to find the file, do:

find ~/Library/Application\ Support/ -name AppName.sqlite

Then, remove the file, and build & run again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜