Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift programming NSErrorPointer error etc

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' 
like image 659
user3732493 Avatar asked Jun 12 '14 04:06

user3732493


2 Answers

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 } 
like image 52
drewag Avatar answered Sep 21 '22 13:09

drewag


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.

like image 27
user1687195 Avatar answered Sep 25 '22 13:09

user1687195