Use NSSerialization.datawithJSON in Swift 2

后端 未结 2 1124
无人共我
无人共我 2021-01-05 17:25

Been trying to get this to work in Swift 2.0, the error says:

Type NSJSONWritingOptions cannot conform to protocol NilLiteralConvertible

相关标签:
2条回答
  • 2021-01-05 17:44
    let options = prettyPrinted ? 
             NSJSONWritingOptions.PrettyPrinted : NSJSONWritingOptions(rawValue: 0)
    

    is the right syntax for swift 2.0

    0 讨论(0)
  • 2021-01-05 17:52

    You can also pass an empty array for no options:

    let options:NSJSONWritingOptions = prettyPrinted ? .PrettyPrinted : []
    
    0 讨论(0)
提交回复
热议问题