gpt4 book ai didi

macos - 仅在 OSX 中使用 Magical Record 创建实体(+entityForName : could not locate an entity named '(null)' in this model.)

转载 作者:行者123 更新时间:2023-12-02 01:57:22 25 4
gpt4 key购买 nike

您好,我在 OSX 中使用 magicRecord 创建实体时遇到问题,我创建了模型和 NSManagagedSubclasses 等等。但是当我在 applicationDidFinishLaunching 中运行这段代码时(我使用的是 magicalRecord 2.2):

[MagicalRecord setupCoreDataStack];
Person * person = [Person MR_createEntity];
person.name = @"alex";

我在日志中得到这个:

2013-10-06 18:01:54.320 TestMagicalRecord[7554:303] +[NSManagedObjectContext(MagicalRecord) MR_contextWithStoreCoordinator:](0x7fff71a96238) -> Created Context UNNAMED
2013-10-06 18:01:54.321 TestMagicalRecord[7554:303] +[NSManagedObjectContext(MagicalRecord) MR_setRootSavingContext:](0x7fff71a96238) Set Root Saving Context: <NSManagedObjectContext: 0x101924d00>
2013-10-06 18:01:54.321 TestMagicalRecord[7554:303] +[NSManagedObjectContext(MagicalRecord) MR_newMainQueueContext](0x7fff71a96238) Created Main Queue Context: <NSManagedObjectContext: 0x1001564a0>
2013-10-06 18:01:54.321 TestMagicalRecord[7554:303] +[NSManagedObjectContext(MagicalRecord) MR_setDefaultContext:](0x7fff71a96238) Set Default Context: <NSManagedObjectContext: 0x1001564a0>
2013-10-06 18:01:54.322 TestMagicalRecord[7554:303] +entityForName: could not locate an entity named '(null)' in this model.

当我在一个 IOS 项目中运行相同的代码时它运行没有问题,我一直在努力寻找 OSX 的示例代码但没有成功,它应该有什么不同吗?

Magical record Github repo 中的 Mac 示例代码只包含一个项目,有没有办法用其他资源运行它?

我敢打赌这是一个简单的解决方案,但我无法在 Docs/SO 中找到任何可行的方法。

最佳答案

同时你也发现了,这是 Magical Record 的开发分支中的一个错误。

在Development分支的2.2版本中,MR_createEntity使用如下方法来确定应该创建的对象的实体名称:

+ (NSString *) MR_bestGuessAtAnEntityName
{
if ([self respondsToSelector:@selector(entityName)])
{
return [self performSelector:@selector(entityName)];
}
return NSStringFromClass(self);
}

但是(至少在 OS X 10.8 上),NSObject 实际上响应 entityName,并调用该方法返回 nil。我找不到任何关于此的文档,所以这可能是一些内部未记录的方法。

但效果是 MR_bestGuessAtAnEntityName 返回 nil,因此

+ (id) MR_createInContext:(NSManagedObjectContext *)context
{
return [NSEntityDescription insertNewObjectForEntityForName:[self MR_bestGuessAtAnEntityName] inManagedObjectContext:context];
}

失败并显示错误消息

+entityForName: could not locate an entity named '(null)' in this model.

所以这是一个 Magical Record 错误,值得向维护者报告。该错误仅发生在 OS X 上,因为 NSObject(或 NSManagedObject)在 iOS 上不响应 entityName

关于macos - 仅在 OSX 中使用 Magical Record 创建实体(+entityForName : could not locate an entity named '(null)' in this model.),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19211133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com