开发者

Reading from Core Data

I want to read some data from Core Data for statistics. My datamodel is like in the tutorials found in the internet (a bit of clicking and voilà it is ready). But now I want to work with the data.

My function:

-(int)calcAve {
    int ret=0;
    ret = [[stats valueForKey:@"aveScore"] intValue];
    NSLog(@"%d",ret);
    return ret;
}

stats is the object connected from the .xib to 开发者_如何学Gothe class, which. This object is bound to the entity Stats in the datamodel. [stats entity] returns the correct value. aveScore is one object in the entity stats (no misspelling, checked it multiple times!). No the error shows me, that stats is not the correct datamodel:

this class is not key value coding-compliant for the key

What is wrong with this? Is there a simple way to read out the data from Core Data?


I am not sure what you managedObject in this case is. However usually you want to store more than one managedObject of the same class in a managedObjectContext. For example several recipes objects in a MOC. Every recipe has the properties.

Your call for value for key sounds reasonable. Check what that class of the stats object is. If it is not of the class NSManagedObject or your custom subclass of it you have a problem. You can only store NSManagedObjects within CoreData. You can check it like this:

NSString *className = NSStringFromClass([stats class]);
NSLog(@"class name: %@",className);

The way you are asking I suggest you try to read the CoreData docs again and look at the Recipes example in Apple's sample code library.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜