14

I want to pass no options when deserializing JSON in Swift (2.0). I originally tried:

NSJSONSerialization.JSONObjectWithData(data, options: nil)

But that doesn't compile, I get the error:

Type NSJSONReadingOptions does not conform to protocol NilLiteralConvertible

The enum NSJSONReadingOptions doesn't have any 'None' option, so what do I do if I don't want of any of these options?

markdb314
  • 5,735
  • 6
  • 25
  • 26

3 Answers3

19

In swift 2 you should use the empty array [] to indicate no options:

NSJSONSerialization.JSONObjectWithData(data, options: [])
luk2302
  • 55,258
  • 23
  • 97
  • 137
  • That seems to work, can you point me to any documentation on why I can pass [ ] when it expects an enumeration? – markdb314 Jul 29 '15 at 19:00
  • @markdb314 i was just looking for a doc link, give me a sec. – luk2302 Jul 29 '15 at 19:07
  • @markdb314 unfortunately I cannot find any doc to why this works, but you can be sure that is the way to go, look at other answer where options are supposed to be given and [] is handed in. I guess it has something to do with `RawOptionSetType` which the **struct** (not enum) `NSKeyValueObservingOptions` conforms to -> http://nshipster.com/rawoptionsettype/ – luk2302 Jul 29 '15 at 19:24
  • Searching OptionSetType on Google (except in Apple documents) will give you the syntax. – keithyip Sep 17 '15 at 04:36
  • How the new OptionSetType works in Switft 2 is explained very well here: http://stackoverflow.com/questions/32169597/swift-2-0-nil-or-0-enum-arguments/32169891#3216989. You can represent it as an array because OptionSetType conforms to ArrayLiteralConvertible – Jessedc Dec 28 '15 at 03:15
  • @ConfusedVorlon are you sure that is the case for swift2? It surely is for swift3 (see the second answer) but I am not so sure about swift2 – luk2302 Oct 24 '16 at 10:39
  • @luk2303 - my apologies. I didn't read your answer carefully enough. Have deleted my comment. – Confused Vorlon Oct 24 '16 at 10:47
8

tldr; Swift 3: Just skip the options param and all will be well.

JSONSerialization.jsonObject(with: data)

Explanation:

in swift 3, the function call is

class func jsonObject(with data: Data, options opt: JSONSerialization.ReadingOptions = []) throws -> AnyObject

ReadingOptions is an option set, the header for Option Set protocol has

/// When you need to create an instance of an option set, assign one of the
/// type's static members to your variable or constant. Alternately, to create
/// an option set instance with multiple members, assign an array literal with
/// multiple static members of the option set. To create an empty instance,
/// assign an empty array literal to your variable.
///
///     let singleOption: ShippingOptions = .priority
///     let multipleOptions: ShippingOptions = [.nextDay, .secondDay, .priority]
///     let noOptions: ShippingOptions = []

The option set docs are here

which means you can call

JSONSerialization.jsonObject(with: data, options: [])

however, the options already has the default [] defined in the function definition, so you can skip it entirely and call

JSONSerialization.jsonObject(with: data)
Confused Vorlon
  • 9,659
  • 3
  • 46
  • 49
1

You can also use an empty constructor for the NSJSONReadingOptions object.

NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
Leo Landau
  • 1,785
  • 17
  • 25