我对这个真的很困惑。网络上有很多问题询问“如何从 Swift 中的 plist 文件获取信息?” 并且到处都发布了相同的答案:
let path = NSBundle.mainBundle().pathForResource("Config", ofType: "plist")
Run Code Online (Sandbox Code Playgroud)
然而,这条生产线总是nil为我生产。我已替换Config为默认 plist 文件中的其他组件,但nil也得到了。
ProductIdentifiers我正在尝试像这样访问我的自定义数组:
let url = NSBundle.mainBundle().URLForResource("ProductIdentifiers", withExtension: "plist")!
var productArray = NSArray(contentsOfURL: url) as! [[String:AnyObject!]]
Run Code Online (Sandbox Code Playgroud)
我在 ProductArray 上收到崩溃提示fatal error: unexpectedly found nil while unwrapping an Optional value。我还尝试过使用其他默认 plist 值来代替ProductIdentifiers.
有谁知道为什么这对我不起作用,尽管周围有很多人成功使用它的帖子?
我以前从未听说过 OP 的方法有效。相反,您应该打开Info.plist文件本身,然后从中提取值,如下所示:
func getInfoDictionary() -> [String: AnyObject]? {
guard let infoDictPath = Bundle.main.path(forResource: "Info", ofType: "plist") else { return nil }
return NSDictionary(contentsOfFile: infoDictPath) as? [String : AnyObject]
}
let productIdentifiers = getInfoDictionary()?["ProductIdentifiers"]
Run Code Online (Sandbox Code Playgroud)
func getInfoDictionary() -> NSDictionary? {
guard let infoDictPath = NSBundle.mainBundle().pathForResource("Info", ofType: "plist") else { return nil }
return NSDictionary(contentsOfFile: infoDictPath)
}
let productIdentifiers = getInfoDictionary()?["ProductIdentifiers"]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1927 次 |
| 最近记录: |