ios - 无法使用类型为 'pathForResource' 的参数列表调用 '(String, ofType: JSON.Type)'

标签 ios json swift

我想要解决问题的方法 错误:

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 TypeFile 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/

相关文章:

swift - 为什么实例方法在 Swift 中有这种类型?

ios - 无法在 swift iOS 中处理 http 代码?

iOS : Programmatically call onTapGesture from another view

android - 在 Android、iOS 和 Blackberry 上嵌入字体——是否有万无一失的解决方案?

swift - 无法从 UITableViewCell 显示 View Controller

swift - updateSearchResultsForSearchController 实现

ios - 如何在 Objective C 中获得手势发生的 CGPoint

javascript - selectize.js 重新加载下拉菜单

java - 我们可以重用 Gson 对象吗?

c# - 手动创建 JSON