快速JSONSerialization中的选项:[] 是什么
let jsonData = try JSONSerialization.data(withJSONObject: updatedJson, options: [])
答案 0 :(得分:3)
JSONSerialization.WritingOptions
是OptionSet
,并且符合ExpressibleByArrayLiteral
协议,这意味着可以将“选项集”指定为数组文字。例如:
let jsonData = try JSONSerialization.data(withJSONObject: json,
options: [.prettyPrinted, .sortedKeys])
特别是,空数组字面量表示“无选择”:
let jsonData = try JSONSerialization.data(withJSONObject: json, options: [])
在这种情况下,它等效于省略参数
let jsonData = try JSONSerialization.data(withJSONObject: json)
因为它的默认值为[]
:
class func data(withJSONObject obj: Any,
options opt: JSONSerialization.WritingOptions = []) throws -> Data