ios - 每次在 iOS 核心数据中获取模型的 nil url

标签 ios

在以下代码中,我总是在 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/

相关文章:

iphone - 如何在 iOS 的 MKAnnotation 中添加更多细节

ios - 字体在UILabel中无法正确显示-裁剪了Ascender或Descender

ios - 具有动态高度的 subview 的自动布局 UIScrollView

ios - 将部署目标设置为 iOS 6 的应用程序瘦身

c# - 如何在 C# 中获取 iOS 设备的广告 ID?

ios - 如何根据顶部正面图像裁剪图像?

ios - FCM - node.js 中的定时器功能

iOS 64 位支持 C 框架 libxml2 "implicit conversion loses integer precision"

ios - 无法在 Swift 中将类型 'UITableViewCell' 的值转换为 'NSIndexPath'

ios - 保存 80 个事件后收到内存警告