在info.plist中添加了自定义键,当我从Swift中的Playground访问时,它为nil [英] added custom key to info.plist, when I access from playground in Swift, it is nil

查看:161
本文介绍了在info.plist中添加了自定义键,当我从Swift中的Playground访问时,它为nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Xcode 7.2(在优胜美地上),我似乎无法从操场上的info.plist访问自定义密钥:

Using Xcode 7.2 (on Yosemite), I can't seem to access custom key from my info.plist from a playground:

import UIKit
var str = "Hello, playground"
let app_id = NSBundle.mainBundle().infoDictionary?["MyAppID"] as? String

使用上面的代码,app_id为nil.我希望它是字符串"TEST"

Using the above code, app_id is nil. I expected it to be the String "TEST"

我将其包括在我的info.plist中,如下所示:

I included it in my info.plist like this:

我还没有听说过的游乐场是否有一些限制?

Is there some limitation to playgrounds that I haven't read about?

如果我打印所有键,则我的自定义键丢失:

If I print all of the keys, my custom key is missing:

let dict = NSBundle.mainBundle().infoDictionary!
for k in dict.keys {
  print("\(k)")
}

打印以下内容:

DTSDKName
CFBundleIdentifier
CFBundleSupportedPlatforms
CFBundleInfoDictionaryVersion
LSRequiresIPhoneOS
CFBundleNumericVersion
DTPlatformName
CFBundleShortVersionString
CFBundleVersion
UIViewControllerBasedStatusBarAppearance
UIRequiredDeviceCapabilities
UIStatusBarHidden
CFBundlePackageType
UIDeviceFamily
CFBundleExecutable
CFBundleInfoPlistURL
LSBackgroundOnly
CFBundleName
CFBundleDevelopmentRegion

如果我查看Bundle名称,就像这样:

If I look at the Bundle name, like this:

let name = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as? String

我看到的是操场的名称,而不是工作区的名称.我可以进入该项目的info.plist吗?

I see the name of my playground, not the name of the workspace. Can I get at the info.plist for the project?

推荐答案

游乐场没有Info.plist文件.操场是独立的.将它们添加到项目中不会导致它们访问该项目中的文件.他们只能访问自己程序包中的文件.

Playgrounds don't have an Info.plist file. Playgrounds are self-contained. Adding them to a project does not cause them to access files in the project. They only can access files in their own package.

您可以将plist文件作为资源添加到游乐场,并以这种方式读取(使用pathForResourceNSDictionary(contentsOfFile:),但不能在其上使用objectForInfoDictionaryKey.您不能将其命名为"Info .plist",因为那样会与自动生成的Info.plist游乐场发生冲突.

You can add a plist file to the playground as a resource and read it that way (using pathForResource and NSDictionary(contentsOfFile:), but you can't use objectForInfoDictionaryKey on it. You can't name it "Info.plist" since that will collide with the auto-genenerated Info.plist playgrounds use to run.

例如,如果将一个plist复制到Resources文件夹中,则将其命名为MyInfo.plist并加载它:

So for example, if you copy a plist into your Resources folder, name it MyInfo.plist, and load it:

let infoPath = NSBundle.mainBundle().pathForResource("MyInfo.plist", ofType: nil)!
let info = NSDictionary(contentsOfFile: infoPath)!

这与真实的Info.plist并不完全相同,因为不会扩展$(EXECUTABLE_NAME)之类的东西.

This isn't exactly the same as a real Info.plist, since things like $(EXECUTABLE_NAME) won't be expanded.

但是这里的要点是,游乐场位于它们自己的捆绑包中,与应用程序无关,它们在运行时动态创建自己的Info.plist.您在这里尝试做的事情与当前游乐场的工作方式不符.

But the points here are that playgrounds are in their own bundle, unrelated to the application and they dynamically create their own Info.plist when they run. What you're trying to do here doesn't match how playgrounds currently work.

理解所有这些的关键部分是游乐场是真实的应用程序.它们被编译并内置到具有其自己的捆绑包的可执行文件中,该捆绑包安装在build目录中.检查NSBundle.mainBundle().bundlePath以查看位置.在游乐场运行时,它不知道其源代码来自何处,更不用说它可能位于其中的更大目录结构了.甚至__FILE__宏也被破坏了(它返回<EXPR>).

A critical part of understanding all of this is that playgrounds are real applications. They get compiled and built into an executable with its own bundle, which is installed into the build directory. Inspect NSBundle.mainBundle().bundlePath to see where. By the time the playground is running, it has no idea where its source code came from, let alone some larger directory structure it might have been in. Even the __FILE__ macro is trashed (it returns <EXPR>).

我希望他们将来会对此有所改进,因为当前有许多类似的事情使他们难以使用它们来探索现有的代码库.它们确实是用于探索少量独立的Swift.我经常发现自己在构建小型命令行应用程序,而不是在操场上.对于非UI工作,命令行应用程序可能会更强大.

I have some hope that they'll improve on this in the future, because currently there are many things like this that make them hard to use to explore existing code bases. They're really for exploring small bits of self-contained Swift. I often find myself building small commandline apps rather than playgrounds. For non-UI work, commandline apps can be more powerful.

这篇关于在info.plist中添加了自定义键,当我从Swift中的Playground访问时,它为nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆