swift编程NSErrorPointer错误等

时间:2014-06-12 04:21:27

标签: swift xcode6 nserror

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, options:NSJSONReadingOptions.AllowFragments, error: &error) as NSDictionary;

会将NSError错误转换为NSErrorPointer。但后来我遇到了一个新的错误,无法理解它:

NSError! is not a subtype of '@|value ST4'

4 个答案:

答案 0 :(得分:68)

自Swift 1以来,这些类型和方法发生了很大变化。

  1. NS前缀已被删除
  2. 这些方法现在抛出异常而不是采用错误指针
  3. 不鼓励使用NSDictionary。而是使用Swift词典
  4. 这导致以下代码:

    do {
        let object = try JSONSerialization.jsonObject(
            with: responseData,
            options: .allowFragments)
        if let dictionary = object as? [String:Any] {
            // do something with the dictionary
        }
        else {
            print("Response data is not a dictionary")
        }
    }
    catch {
        print("Error parsing response data: \(error)")
    }
    

    如果您不关心特定的解析错误:

    let object = try JSONSerialization.jsonObject(
        with: responseData,
        options: .allowFragments)
    if let dictionary = object as? [String:Any] {
        // do something with the dictionary
    }
    else {
        print("Response data is not a dictionary")
    }
    

    原始答案

    您的NSError必须定义为Optional,因为它可以是nil:

    var error: NSError?
    

    您还要考虑解析中存在错误,该错误将返回nil或解析返回数组。为此,我们可以使用as?运算符进行可选的转换。

    这给我们留下了完整的代码:

    var possibleData = NSJSONSerialization.JSONObjectWithData(
        responseData,
        options:NSJSONReadingOptions.AllowFragments,
        error: &error
        ) as? NSDictionary;
    
    if let actualError = error {
        println("An Error Occurred: \(actualError)")
    }
    else if let data = possibleData {
       // do something with the returned data
    }
    

答案 1 :(得分:11)

如上所述,错误必须定义为可选(https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/buildingcocoaapps/AdoptingCocoaDesignPatterns.html

但是 - 如果出现错误并返回nil,此代码将崩溃,“As NSDictionary”将成为罪魁祸首:

var data: NSDictionary =
 NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: &error) as NSDictionary;

你需要像这样进行json解析:

var jsonError : NSError?

let jsonResult : AnyObject? = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &jsonError)

if let error = jsonError{
    println("error occurred: \(error.localizedDescription)")
}
else if let jsonDict = jsonResult as? NSDictionary{
    println("json is dictionary \(jsonDict)")
}
else if let jsonArray = jsonResult as? NSArray{
    println("json is an array: \(jsonArray)")
}

那会有效。还记得json可以作为一个数组返回。您可以传递任何您喜欢的内容,而不是为选项传递nil,例如:

NSJSONReadingOptions.AllowFragments

如果你愿意的话。

答案 2 :(得分:1)

现在处于beta-3

var error: AutoreleasingUnsafePointer<NSError?> = nil

var data: NSDictionary = NSJSONSerialization.JSONObjectWithData( w, options:.AllowFragments, error: error ) as NSDictionary;

答案 3 :(得分:0)

使用以下代码检查:

var error: AutoreleasingUnsafePointer<NSErrorPointer?>=nil
var dataVal: NSData =  NSURLConnection.sendSynchronousRequest(request1, returningResponse: response, error:nil)
var err: NSError?
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
    println("Result\(jsonResult)")

尝试使用

 var error: AutoreleasingUnsafePointer<NSErrorPointer?>=nil