var data: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData,options:NSJSONReadingOptions.AllowFragments,error: error) as NSDictionary;
NSError is not convertable to NSErrorPointer.
所以我随后想到将代码更改为:
var data: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData,error: &error) as NSDictionary;
这将把NSError错误转换为NSErrorPointer。但是,然后我得到一个新的错误,不能理解它:
NSError! is not a subtype of '@|value ST4'
您的NSError必须定义为可选,因为它可以是nil:
var error: NSError?
您还想要解释在解析中有错误,它将返回nil或解析返回一个数组。要做到这一点,我们可以使用一个可选的铸造与as?运算符。
这给我们留下了完整的代码:
var possibleData = NSJSONSerialization.JSONObjectWithData( responseData,error: &error ) as? NSDictionary; if let actualError = error { println("An Error Occurred: \(actualError)") } else if let data = possibleData { // do something with the returned data }