迅捷:无法在操场上将(本地)plist文件读取到字典中 [英] swift: could not read (local) plist file to Dictionary in a playground

查看:161
本文介绍了迅捷:无法在操场上将(本地)plist文件读取到字典中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.我创建了一个新的游乐场,并将我的2.plist添加到游乐场的包内容文件夹中. 2.我尝试过:

1.I creat a new playground,and add my 2.plist to the playground's package content folder. 2.I tried:

var myDict: NSDictionary?
if let path = NSBundle.mainBundle().pathForResource("2", ofType:"plist"){
println(path)// "/var/folders/....../2.plist"

myDict = NSDictionary(contentsOfFile: path) as? Dictionary<String, AnyObject>
println(myDict)// nil

let url:NSURL! = NSBundle.mainBundle().URLForResource("2", withExtension:"plist")
myDict = NSDictionary(contentsOfURL: url)
println(myDict) //nil

var str = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)! 
// ??? here display the String content of 2.plist
} else {
println("exist")
}

3.我尝试了很多方法,但是无法将plist读到字典中.为什么?

3.I have tried many ways, but could not read the plist to dictionary.Why???

非常感谢您的帮助.

推荐答案

我认为您错误地添加了Plist文件,因此请执行以下步骤:

I think you are adding your Plist file wrongly so follow this step:

文件检查器

在XCode菜单中,只需选择查看->实用程序->显示文件检查器:

In the XCode menu just select View -> Utilities -> Show File Inspector:

您会在右侧看到一个面板. 操场设置部分将提供您到资源文件夹的路径.只需单击小箭头,然后打开资源"文件夹!

You’ll see a panel on the right hand side. The Playground Settings section will have your path to the Resource folder. Just click the little arrow, and open up the Resource folder!

现在,只需将您的plist文件添加到Resources文件夹中,然后编码即可!

Now just add your plist file in the Resources folder, and code away!

在此步骤之后,您可以在这里看到代码运行正常.

Here you can see you code is working perfectly after following this steps.

这是我的plist格式,以获取更多信息:

Here is my plist format for more information:

此处参考 希望对您有帮助.

Reference from here Hope this will help you.

这篇关于迅捷:无法在操场上将(本地)plist文件读取到字典中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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