0
我一直在努力阅读plist文件到我的iOS应用程序,但是我迄今使用的每种方法都失败了。我目前使用的方法描述在http://basememara.com/reading-values-plist-bundle-swift/,但在我尝试的每种方法似乎都有同样的问题。变量不会填充plist数据,Swift
如果这是阅读的plist文件中的代码:
/**
Gets the contents of the specified plist file.
- parameter plistName: property list where defaults are declared
- parameter bundle: bundle where defaults reside
- returns: dictionary of values
*/
public static func contentsOfFile(plistName: String, bundle: Bundle? = nil) -> [String : AnyObject] {
let fileParts = plistName.components(separatedBy: ".")
guard fileParts.count == 2,
let resourcePath = (bundle ?? Bundle.main).path(forResource: fileParts[0], ofType: fileParts[1]),
let contents = NSDictionary(contentsOfFile: resourcePath) as? [String : AnyObject]
let contentsarray = NSArray(contentsOfFile: resourcePath)
else { return [:] }
return contents
}
和呼叫
let values = Bundle.contentsOfFile(plistName: "Event.plist")
print(values["Item0"]) // My string value 1.
变量内容和ContentsArray(用于测试)没有任何东西。我已经验证了resourcePath是正确的,并且没有给出错误,所以我不知道什么是错的。其他每种方法都有正确的路径,但永远不会填充内容变量。
检查文件的目标成员资格,并在finder中存在它说它会看起来,但这两种方法打印零。 – traisjames
文件的位置是什么?它是在主包还是从某个地方下载它,同时保存到其他目录,如缓存? – Avi
它在主包中(.../6922A277-91D7-4F70-A03F-0DD6541C89EF/LG%20Sim%20Builder.app/Event.plist) – traisjames