坠毁在发布模式下,当加载plistCrash in release mode when loading a plist

- 此内容更新于:2015-01-27
主题:

我一直在加载数据的plist好几个视图控制器使用这段代码: 这在调试模式下运行正常,但就我为释放模式,构建应用程序崩溃时试图访问countryArray,说这是零。 什么好主意吗? 谢谢 编辑 如果我试着记录数组使用 NSLog(“(NSArray(contentsOfFile:路径))”) 我得到正确的值。

原文:

I have been loading data from a plist fine in a couple view controllers using this code:

if let path = NSBundle.mainBundle().pathForResource("eBaySiteIDs", ofType: "plist") {

        if let dict = NSArray(contentsOfFile: path) as? Array<AnyObject> {
            // use swift dictionary as normal
            countryArray = dict as? [[String : AnyObject]]
        }
    }

This runs properly in debug mode, but as soon I build for release mode, the app crashes when trying to access the countryArray, saying it is nil.

Any ideas?

Thanks

EDIT

If I try to log the array using

NSLog("(NSArray(contentsOfFile: path))")

I get the correct values.

解决方案:
你的问题是,你没有添加资源构建阶段。所以它可能在调试模式下工作,但在发布模式下,文件丢失。 所以,你必须添加视频Xcode目标下副本包资源的构建阶段。
原文:

Your problem is, that you don't have added your resource to your Build Phase yet. So it may work in debug mode, but in release mode, the file is missing.

So, you have to add the video in your Xcode target under Build Phases to the Copy Bundle Resources.

Steven Ritchie的回复:# 39;实质性的年代已经在那里

(原文:It's already in there)

解决方案:
解决了。 因为某些原因我需要改变语句:
原文:

Solved.

For some reason I needed to change the statement to:

if let dict : AnyObject = NSArray(contentsOfFile: path) {

}