swift programming NSErrorPointer error etc

2019-02-07 16:42发布

问题:

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

This line of code gives me error

NSError is not convertable to NSErrorPointer.

So I then thought to change the code to:

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

which would turn the NSError error into a NSErrorPointer. But then I get a new error and cannot make sense of it:

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

回答1:

These types and methods have changed a lot since Swift 1.

  1. The NS prefix is dropped
  2. The methods now throw exceptions instead of taking an error pointer
  3. Use of NSDictionary is discouraged. Instead use a Swift dictionary

This results in the following code:

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

Of, if you don't care about the specific parsing 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")
}

Original Answer

Your NSError has to be defined as an Optional because it can be nil:

var error: NSError?

You also want to account for there being an error in the parsing which will return nil or the parsing returning an array. To do that, we can use an optional casting with the as? operator.

That leaves us with the complete code:

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
}


回答2:

As mentioned, the error must be defined as optional (https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/buildingcocoaapps/AdoptingCocoaDesignPatterns.html)

However - This code WILL CRASH if there is an error and nil is returned, the "As NSDictionary" would be the culprit:

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

You need to do the json parsing like this:

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

That will work. Also remember json can come back as an array. Instead of passing nil for the options you can pass whatever you like, for example:

NSJSONReadingOptions.AllowFragments

if you like.



回答3:

Now in beta-3

var error: AutoreleasingUnsafePointer<NSError?> = nil

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


回答4:

Check With below code:

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)")

Try using

 var error: AutoreleasingUnsafePointer<NSErrorPointer?>=nil