views:

74

answers:

1

Ok so I'm trying to build this stat tracker for my app and I have built a data model object called statTracker that keeps track of all the stuff I want it to. I can set and retrieve values using the selectors, but if I try and use KVC (ie setValue: forKey: ) everything goes bad and says my StatTracker class is not KVC compliant:

valueForUndefinedKey:]: the entity StatTracker is not key value coding-compliant for the key "timesLauched".' 2010-05-18 15:55:08.573

here's the code that is triggering it:

NSArray *statTrackerArray = [[NSArray alloc] init];
statTrackerArray = [[CoreDataSingleton sharedCoreDataSingleton] getStatTracker];

NSNumber *number1 = [[NSNumber alloc] init];

number1 = [NSNumber numberWithInt:(1 + [[(StatTracker *)[statTrackerArray objectAtIndex:0] valueForKey:@"timesLauched"] intValue])];
[(StatTracker *)[statTrackerArray objectAtIndex:0] setValue:number1 forKey:@"timesLaunched" ];

NSError *error;
if (![[[CoreDataSingleton sharedCoreDataSingleton] managedObjectContext] save:&error]) {
    NSLog(@"error writing to db");
}

Not sure if this is enough code for you folks let me know what you need if you do need more.

This would be so sweet if I could use KVC because I could then abstract all this stat tracking stuff into a single method call with a string argument for the value in question. At least that is what I hope to accomplish here. I'm actually now understanding the power of KVC but now I'm just trying to figure out how to make it work.

Thanks!

Nick

After adding the code suggested below the output is this:

Object: (entity: StatTracker; id: 0x3e1e1b0 ; data: ) 2010-05-19 11:30:38.173 verses[29526:207] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: the entity StatTracker is not key value coding-compliant for the key "timesLauched".' 2010-05-19 11:30:38.174 verses[29526:207] Stack: (

+1  A: 

Ok, first there are a couple of errors in the code. The NSArray and the NSNumber that you are calling alloc init on are just being thrown away by the assignment on the next line. Therefore those should start out life as a nil (or you can combine the two lines together).

Second you should grab a pointer reference to the NSManagedObject so that you can then peek at it and make sure it is what you think it is.

NSArray *statTrackerArray = nil;
statTrackerArray = [[CoreDataSingleton sharedCoreDataSingleton] getStatTracker];

NSNumber *number1 = nil;

id object = [statTrackerArray objectAtIndex:0];
NSLog(@"Object: %@", object);
NSInteger timesLaunched = [[object valueForKey:@"timesLauched"] intValue];
timesLaunched += 1;
number1 = [NSNumber numberWithInteger:timesLaunched];

[object setValue:number1 forKey:@"timesLaunched" ];

NSError *error = nil;
if (![[[CoreDataSingleton sharedCoreDataSingleton] managedObjectContext] save:&error]) {
  NSLog(@"error writing to db: %@\n%@", [error localizedDescription], [error userInfo]);
}

Those changes will remove the memory leaks and will let you see exactly what you are trying to work with. I also unrolled your incrementing of the number so that when you run this in the debugger with a breakpoint on objc_exception_throw you can see exactly which line is causing the issue.

I suspect that your object StatTracker is not a proper subclass of NSManagedObject.

Change the code to match what I have above and re-run the test. Then update your question with the output so that we can get a better look at the issue.

Update

The property is 'timesLaunched'

You are trying to access 'timesLauched'

Simple typo. I even copied your typo into my version of your code :)

Marcus S. Zarra
Hey, thanks for looking at this, I'm really in over my head with this app. Or at least that is how it feels right now, I need to build a bunch of pieces that I am not even sure how to do. I really appreciate the help. As far as the StatTracker class, I just built it using the xcode modeler didn't write one line of code inside it, it's possible that it added it as a cocoa managedObject and not a UIKit object model not sure if that would have made a difference such as we're seeing but I'll try it. -thanks again, nick
nickthedude
same result doing a "rebuild" of the StatTracker object.
nickthedude
If you added the code I have above you should have a new log statement that starts with "Object:" what does that line say?
Marcus S. Zarra
2010-05-19 11:37:29.110 verses[29592:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<StatTracker 0x3b0e8d0> valueForUndefinedKey:]: the entity StatTracker is not key value coding-compliant for the key "timesLauched".'2010-05-19 11:37:29.111 verses[29592:207] Stack: (
nickthedude
OMG, i looked at that for like a minute and still couldn't see it. thank you so much, I thought it might be something lame like that, it seemed like I was doing it the right way. Thanks again Marcus, hope I can help you out in some way in the future. --nick
nickthedude