In Objective-C I wrote code like the following:
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:someDictionary
options:0
error:&error];
I am trying to do the same thing in Swift. With some prompting from Xcode’s syntax checking I wrote it like this:
var jsonError: NSError
let jsonData = NSJSONSerialization.dataWithJSONObject(someDictionary, options: NSJSONWritingOptions(), error: &jsonError)
but this gives me an error: “Could not find an overload for init
that accepts the supplied arguments.” I think the problem might be with the NSJSONWritingOptions()
bit, and I’m guessing I just have the Swift syntax wrong. I tried replacing NSJSONWritingOptions()
with NSJSONWritingOptions(0)
and got the same error; I tried replacing it with nil
(as suggested by this answer) but I got the error “Could not find an overload for __conversion
that accepts the supplied arguments.”
How can I indicate that I want the default JSON writing options, whatever those might be?
The problem is not the NSJSONWritingOptions; the type of error you're passing should be NSError?
, not NSError
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With