ios - NSBundle mainBundle 返回 nil
问题描述
我正在尝试根据我们 iOS 应用程序的 Xcode 中的构建配置更改服务器设置。我以本指南为基础。我在 Info.plist 中设置了配置属性,但是当我尝试在代码中获取它时它又回来了nil
。
在寻找解决方案和测试时,我发现我正在nil
从与mainBundle
. 到目前为止,这是我在测试中声明的内容。我错过了什么/做错了什么?
NSBundle *bundle = [NSBundle mainBundle];
NSString *configuration = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"Configuration"];
NSString *bundleName = [[NSBundle mainBundle] bundleIdentifier];
NSString *otherName = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleNameKey];
我正在使用 Xcode 9.3。
解决方案
在某些情况下,例如当您的代码直接从 Interface Builder 中执行时,mainBundle
不会返回对象。因此,根据您的上下文,您可能想要使用它bundleForClass:
。指定驻留在所需捆绑包中的类。
NSBundle *bundle = [NSBundle bundleForClass:[YourClass class]];
推荐阅读
- javascript - 使用 javascript 代理跟踪方法调用
- javascript - 为什么我的 process.env 变量未定义?
- vba - 使用 VBA 在 url 中保留空格
- python - 在 Django 视图中导入 celery 任务
- c# - 如何将 Automapper IMappingAction 与 asp.net bolierplate 一起使用?
- ruby - Redmine 不显示 Git 修订
- node.js - Mongodb 文档中不存在的字段出现在 mongoose findById() 结果中
- mysql - 跨不同日期的 MySQL 选择语句
- php - Plesk Onyx 安装 Owncloud / Nextcloud 错误“未安装 PHP 模块 zip”
- java - 如何使用 scened2d 为滚动列表创建项目