swift programming NSErrorPointer error etc

后端 未结 4 2060
我在风中等你
我在风中等你 2021-01-01 08:23
var data: NSDictionary = 
    NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: error) as NSDictionary;


        
相关标签:
4条回答
  • 2021-01-01 09:03

    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
    }
    
    0 讨论(0)
  • 2021-01-01 09:10

    Now in beta-3

    var error: AutoreleasingUnsafePointer<NSError?> = nil
    
    var data: NSDictionary = NSJSONSerialization.JSONObjectWithData( w, options:.AllowFragments, error: error ) as NSDictionary;
    
    0 讨论(0)
  • 2021-01-01 09:11

    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
    
    0 讨论(0)
  • 2021-01-01 09:25

    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.

    0 讨论(0)
提交回复
热议问题