📄
Henry
  • Henry的神秘小屋
  • 其他
    • 字节码与机器码的区别?
  • ObjectiveC
    • NSInvocation
    • 检测OC项目中未使用的方法
    • Method Selector
    • 消息转发
  • Swift
    • 检测Swift项目中未使用的类 方法 属性
    • NSCoding
    • Mirror
    • JSONEncode JSONDecode
    • Any AnyObject AnyClass
    • is as? as! as
  • Cocoapods
    • 看懂Podfile.lock文件
    • 在编写的Pod中使用宏预编译
  • iOS
    • 苹果应用 保持画面流畅
    • 关于计算机展示图像的一些问题
    • @testable
    • iOS中URLRequest的缓存策略
    • CodePush接入流程
    • H5在WKWebView中读取沙盒文件
    • FDLog App客户端日志系统
    • 如何实现JSBridge基于WKWebView
    • 网络请求各个指标的度量
    • iOS13 UIModalPresentationStyle
    • 实现H5离线包机制
    • NSURLProtocol 拦截器
    • Framework
    • Lock
    • CFNetwork NSURLSession NSURLConnection
    • setNeedsLayout layoutIfNeeded layoutSubviews
    • StackView
    • Flutter Method Channel:从Dart到Native调用链
    • JSONSerialization.ReadingOptions
    • JSONSerialization.WritingOptions
    • RunLoop高级2
    • RunLoop高级1
    • RunLoop中级
    • RunLoop初级
    • LineBreak AutoShrink
    • 如何给H5出WebView调试包
    • TODO、FIXME、!!!、???、MARK
    • Operation的使用
    • UserDefault
    • 了解WKWebView
    • 输出日志信息到系统控制台
    • Float Double 失去精度问题
    • 使用xcodebuild命令打包导出
    • 在iOS项目中使用C++定义的模块
    • 证书问题
    • 创建常驻线程
  • 源码
    • 阅读PINCache源码
    • 解读AspectHook
    • HandyJSON是如何实现的?
  • 汇编
    • 看懂汇编
由 GitBook 提供支持
在本页
  • 介绍 ReadingOptions
  • 验证

这有帮助吗?

  1. iOS

JSONSerialization.ReadingOptions

介绍 ReadingOptions

这三个 case

  • mutableContainers 得到一个可变对象

  • mutableLeaves JSON 的叶子节点如果是 String 类型,会被创建成可变类型的 String

  • fragmentsAllowed 除了 Map 和 Arrary 这两个数据结构可以作为 JSON 顶层,还允许其他类型作为顶层,比如 string,number,null,bool 。

    @available(iOS 5.0, *)
    public struct ReadingOptions : OptionSet {

        public init(rawValue: UInt)


        public static var mutableContainers: JSONSerialization.ReadingOptions { get }

        public static var mutableLeaves: JSONSerialization.ReadingOptions { get }

        public static var fragmentsAllowed: JSONSerialization.ReadingOptions { get }

        @available(iOS, introduced: 5.0, deprecated: 100000, renamed: "JSONSerialization.ReadingOptions.fragmentsAllowed")
        public static var allowFragments: JSONSerialization.ReadingOptions { get }
    }

验证

一,mutableContainers

验证所有类型后,得到结论,Map 和 Arrary 类型转换后可以得到可变类型,其他类型 比如,单独的 string,单独的 number 都不可以。

1.得到可变对象 __NSDictionaryM

let str = "{\"name\":\"henry\"}"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

// 输出
__NSDictionaryM
{
    name = henry;
}

2.得到不可变对象 __NSSingleEntryDictionaryI

let str = "{\"name\":\"henry\"}"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = []
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

// 输出
__NSSingleEntryDictionaryI
{
    name = henry;
}

二,allowFragments

验证后,得到结论是: bool,number,null,string,map,arrary 都可以做 JSON 的最外层结构。

  1. bool 类型

let str = "true"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

//输出
__NSCFBoolean
1
  1. number 类型

let str = "1.2"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

//输出
NSDecimalNumber
1.2
  1. string 类型

let str = "\"1.2\""
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

//输出
NSTaggedPointerString
1.2
  1. null 类型

let str = "null"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

//输出
NSNull
<null>
  1. arrary 类型

let str = "[\"1\"]"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

//输出
__NSArrayM
(
    1
)
  1. object 类型

let str = "{\"1\":\"2\"}"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments]
let json = try! JSONSerialization.jsonObject(with: data!, options: options)
print(type(of: json))
print(json)

//输出
__NSDictionaryM
{
    1 = 2;
}

三,mutableLeaves

苹果官方文档描述这个 Case: Specifies that leaf strings in the JSON object graph are created as instances of NSMutableString.

做个实验:

let str = "{\"1\":\"2\"}"
let data = str.data(using: .utf8)
let options: JSONSerialization.ReadingOptions = [.mutableContainers,.allowFragments,.mutableLeaves]
let json = try! JSONSerialization.jsonObject(with: data!, options: options) as! [String: Any]
print(type(of: json["1"]!))
print(json)

//输出
NSTaggedPointerString
["1": 2]

确实没有像文档说的那样会创建一个 NSMutableString,不过对我们使用 swift 的童鞋木有啥影响。

上一页Flutter Method Channel:从Dart到Native调用链下一页JSONSerialization.WritingOptions

最后更新于5年前

这有帮助吗?

翻译过来就是: JSON 的叶子数据,如果是 string 类型,转换过来会被转换成 NSMutableString,但是,我不论是在 swift 还是 oc 都是不行,确实大家也都遇到了。 比如

https://link.jianshu.com/?t=http://stackoverflow.com/questions/19345864/nsjsonreadingmutableleaves-option-is-not-working