What do JSONSerialization options do and how do they change jsonResult?

前端 未结 3 603
遇见更好的自我
遇见更好的自我 2020-12-05 04:35

I am using JSONSerialization quite often in my project. Here is an example of my JSONSerialization code:

let json = try JSONSeriali         


        
3条回答
  •  感情败类
    2020-12-05 05:18

    Short answer for the first two options:

    Ignore them in Swift!

    In Swift you can make objects mutable just with the var keyword.

    In Objective-C on the other hand you need

    • NSJSONReadingMutableContainers to make the nested collection types mutable NSArrayNSMutableArray and NSDictionaryNSMutableDictionary.
    • NSJSONReadingMutableLeaves to make the value strings mutable → NSMutableString.

    In both Objective-C and Swift if you are only reading the JSON you don't need mutability at all.

    The third option NSJSONReadingAllowFragments is important if the root object of the received JSON is not an array and not a dictionary.
    If it is an array or dictionary you can omit that option, too.

    The pair of empty brackets [] represents No options (the options parameter can be omitted in Swift 3+).

提交回复
热议问题