在以下代码中,我总是在 url
中得到 nil
:
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSURL *url = [bundle URLForResource:@"Model" withExtension:@"momd"];
bundle
不是 nil
,我的模型名称是 Model.xcdatamodeld
可能出了什么问题?
最佳答案
我认为bundleForClass返回给您的内容并不是显而易见的。苹果的docs说:
- (NSBundle *)bundleForClass:(Class)aClass
Return Value:
The NSBundle object that dynamically loaded aClass (a loadable bundle), the NSBundle object for the framework in which aClass is defined, or the main bundle object if aClass was not dynamically loaded or is not defined in a framework.
它只会在某些情况下为您提供 mainBundle。你没说你的类(class)是什么。我认为直接进入 mainBundle (就像你所做的那样)要安全得多。
关于ios - 每次在 iOS 核心数据中获取模型的 nil url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35129093/