我想要解决问题的方法 错误:
Cannot invoke 'pathForResource' with an argument list of type '(String, ofType: JSON.Type)'
我的代码是这样的:
func parsejson() {
let path : String? = NSBundle.mainBundle().pathForResource("ooo", ofType: JSON) as String!
// let jsonData = NSData(contentsOfFile: path) as JSON!
// let readableJson = JSON(data: jsonData, options: NSJSONReadingOptions.MutableContainers, error: nil)
}
最佳答案
pathForResource:ofType
方法早在引入 Swift 之前就已经实现了。当时操作系统使用File Type
和File Creator
实际上它的意思是pathForResource:withExtension
,就像URL相关对应的签名。
无论如何我推荐使用 URL 相关的 API
if let url = NSBundle.mainBundle().URLForResource("ooo", withExtension: "json") {
let jsonData = NSData(contentsOfURL: url)! // it's NSData not JSON
let readableJson = JSON(data: jsonData, options: NSJSONReadingOptions.MutableContainers, error: nil)
} else {
fatalError("The resource does not exist")
}
关于ios - 无法使用类型为 'pathForResource' 的参数列表调用 '(String, ofType: JSON.Type)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33583590/