ios – `NSBundle.mainBundle().URLForResource`总是返回`nil`

ios – `NSBundle.mainBundle().URLForResource`总是返回`nil`,第1张

概述我是 Swift的新手,正在使用Xcode 6. 我试图从应用程序的plist文件中读取数据,但它无法正常工作. data.plist文件包含在Xcode的Supporting Files组中. 我使用下面的代码: var dataList = NSDictionary(contentsOfURL:NSBundle.mainBundle().URLForResource("data", withE 我是 Swift的新手,正在使用Xcode 6.

我试图从应用程序的pList文件中读取数据,但它无法正常工作.

data.pList文件包含在Xcode的Supporting files组中.

我使用下面的代码:

var dataList = NSDictionary(contentsOfURL:NSBundle.mainBundle().URLForResource("data",withExtension:"pList"))

然而NSURL:

NSBundle.mainBundle().URLForResource("data",withExtension:"pList")

总是返回零.

我不知道出了什么问题.

解决方法 通常,您会希望使用此代码来创建pList.这将找到pList的路径,然后将其移动到文档目录中(如果尚未存在).如果你不移动它,就不允许你写它,因此这段代码是至关重要的.要从pList中获取信息,请使用第二位代码.显然,如果你有一个数组而不是一个字典,你将不得不改变它来处理它.

var path = NSSearchPathForDirectorIEsInDomains(.documentDirectory,.UserDomainMask,true)[0] as Nsstring    path = path.stringByAppendingPathComponent("data.pList")    let fileManager = NSfileManager.defaultManager()    if !fileManager.fileExistsAtPath(path) {        let sourcePath = NSBundle.mainBundle().pathForResource("data",ofType: "pList")        fileManager.copyItemAtPath(sourcePath,topath: path,error: nil)}

.

let dict = NSMutableDictionary(contentsOffile: path) as NSMutableDictionary
总结

以上是内存溢出为你收集整理的ios – `NSBundle.mainBundle().URLForResource`总是返回`nil`全部内容,希望文章能够帮你解决ios – `NSBundle.mainBundle().URLForResource`总是返回`nil`所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1024407.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存