开发者

iPhone OS: Why is my managedModelObject not complying with Key Value Coding?

开发者 https://www.devze.com 2022-12-30 14:24 出处:网络
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

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 t开发者_如何学JAVAo 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: (


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 :)

0

精彩评论

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

关注公众号