2015-11-07 48 views
1

我想一个解决问题的办法 errror:不能援引“pathForResource”类型的参数列表“(字符串,ofType:JSON.Type)”

不能援引“pathForResource”与参数列表 型 '(字符串,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) 
} 
+0

什么是'JSON'?它存储的值是多少? –

+0

https://www.youtube.com/watch?v=_NfijT6mt6A –

回答

0

方法pathForResource:ofType已经很久以前在Swift推出之前实现了。当时操作系统使用的术语如File TypeFile Creator

实际上它的意思是pathForResource:withExtension就像URL相关对应的签名一样。

无论如何,我建议使用相关的API的URL

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") 
} 
+0

非常感谢问题解散 –

0

您至少有两个埃罗rs在这里。

首先,您尝试设置ofType,即String?类型为JSON类型。看起来像你使用SwiftyJSON,并从那里输入。您需要将其转换为String对象。

其次,您不能作为参数传递类类型,您必须创建该类的一些对象,然后传递它。

+0

我使用SwiftyJSON liabery,“ooo”是项目中的文件json :) –

+0

这个问题,对某些人来说是真的,我看过像我这样的视频工作8 :00,https://www.youtube.com/watch?v=_NfijT6mt6A –

+0

@Aslamdodeen在这段视频中,'ofType'将字符串“json”设置为类似于@vadian的回答,但不是类型“JSON” –

相关问题