Fast error programming NSErrorPointer etc.

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

This line of code gives me an error

 NSError is not convertable to NSErrorPointer. 

So, I thought to change the code to:

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

which turned the NSError error into NSErrorPointer. But then I get a new error and cannot understand it:

 NSError! is not a subtype of '@|value ST4' 
+42
swift xcode6 nserror
Jun 12 '14 at 4:21
source share
4 answers

Your NSError must be defined as Optional because it can be nil:

 var error: NSError? 

You also want to consider the presence of a parsing error that will return nil or parsing that returns an array. To do this, we can use the optional casting with the as? operator as? .

This leaves us with the full 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 } 
+67
Jun 12 '14 at 4:28
source share

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

However - this code WILL BE FORBIDDEN if an error occurs and nil is returned, "As NSDictionary" will be the culprit:

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

You need to do json parsing as follows:

 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 that json can be returned as an array. Instead of passing zero for parameters, you can pass whatever you like, for example:

 NSJSONReadingOptions.AllowFragments 

if you want to.

+11
Jun 20 '14 at 19:01
source share

Now in beta 3

 var error: AutoreleasingUnsafePointer<NSError?> = nil var data: NSDictionary = NSJSONSerialization.JSONObjectWithData( w, options:.AllowFragments, error: error ) as NSDictionary; 
+1
Jul 18 '14 at 5:31
source share

Check with the code below:

 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
Jun 12 '14 at 4:34
source share



All Articles