iPhone OS:为什么我的 ManagedModelObject 不符合键值编码?

发布于 2024-09-01 16:45:47 字数 1356 浏览 7 评论 0原文

好吧,我正在尝试为我的应用程序构建这个统计跟踪器,并且我已经构建了一个名为 statTracker 的数据模型对象,它可以跟踪我想要的所有内容。我可以使用选择器设置和检索值,但是如果我尝试使用 KVC (即 setValue: forKey: ),一切都会变坏,并说我的 StatTracker 类不符合 KVC 标准:

valueForUndefinedKey:]: 实体 StatTracker 不是键值编码 -符合关键“timesLauched”。 2010-05-18 15:55:08.573

这是触发它的代码:

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

不确定这对你们来说是否足够,如果您确实需要更多,请让我知道您需要什么。

如果我可以使用 KVC,那就太棒了,因为我可以将所有这些统计跟踪内容抽象为单个方法调用,并使用相关值的字符串参数。至少这是我希望在这里实现的目标。实际上,我现在已经了解了 KVC 的强大功能,但现在我只是想弄清楚如何使其发挥作用。

谢谢!

添加下面建议的代码后,输出如下

对象:(实体:StatTracker;id:0x3e1e1b0;数据:) 2010-05-19 11:30:38.173 verses[29526:207] *** 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[ valueForUndefinedKey:]:实体 StatTracker 与键的键值编码不兼容“timesLauched”。 2010-05-19 11:30:38.174 诗句[29526:207] 堆栈:(

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2024-09-08 16:45:47

好的,首先代码中有一些错误。您正在调用 alloc init 的 NSArrayNSNumber 刚刚被下一行的赋值丢弃。因此,这些应该以 nil 开始(或者您可以将这两行组合在一起)。

其次,您应该获取对 NSManagedObject 的指针引用,以便您可以查看它并确保它就是您所认为的那样。

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

这些更改将消除内存泄漏,并让您准确地看到您正在尝试使用的内容。我还展开了数字的递增,以便当您在调试器中使用 objc_exception_throw 上的断点运行此代码时,您可以准确地看到哪一行导致了问题。

我怀疑您的对象 StatTracker 不是 NSManagedObject 的正确子类。

更改代码以匹配上面的代码并重新运行测试。然后用输出更新您的问题,以便我们可以更好地了解问题。

更新

属性是“timesLaunched”

您正在尝试访问“timesLauched”

简单的拼写错误。我什至将您的拼写错误复制到我的代码版本中:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文