Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for passing “no options” to a parameter of type NSJSONWritingOptions

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?

like image 676
bdesham Avatar asked Mar 19 '23 09:03

bdesham


1 Answers

The problem is not the NSJSONWritingOptions; the type of error you're passing should be NSError?, not NSError.

like image 192
Jesse Rusak Avatar answered Apr 24 '23 16:04

Jesse Rusak