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'
These types and methods have changed a lot since Swift 1.
NS
prefix is droppedThis results in the following code:
Of, if you don't care about the specific parsing error:
Original Answer
Your NSError has to be defined as an
Optional
because it can be nil: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 theas?
operator.That leaves us with the complete code:
Now in beta-3
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:
You need to do the json parsing like this:
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:
if you like.
Check With below code:
Try using