swift - NSJSONWritingOptions 解释

标签 swift

使用:

NSJSONSerialization.dataWithJSONObject(array, options: NSJSONWritingOptions(rawValue:2))

你能告诉 med rawValue 的用途吗?

最佳答案

rawValue 位来自 OptionSetType , NSJSONWritingOptions 继承自它。

很可能你应该完全避免这种情况,而是使用标准的 Swift 枚举语法,它提供了一个很好的短常量名称:

NSJSONSerialization.dataWithJSONObject(array, options: .PrettyPrinted)

编辑:

要不指定任何选项或指定多个选项,您可以使用恢复到 rawValue 语法并像在 C 中那样自行计算值,但更简单的选项是下面的数组样式语法:

不指定任何选项:

NSJSONSerialization.dataWithJSONObject(array, options: [])

组合多个选项:

NSJSONSerialization.JSONObjectWithData(data, options: [.MutableContainers, .MutableLeaves])

关于swift - NSJSONWritingOptions 解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33768369/

相关文章:

ios - 在sceneDidEnterBackground(iOS13)上的URL请求

ios - 类变量值未更新

iphone - 应用程序中的 EXC_BAD_ACCESS 错误,Alamofire 3.1.2

arrays - 通过唯一字典值过滤字典数组的简洁方法

swift - 无效更新 : invalid number of rows in section 0, 更新后现有节中包含的行数

swift - NSFetchRequest 找不到实体名称 'Article' 的 NSEntityDescription '

ios - 如何在 swift iOS 中为跟随 JSON 响应制作模型类

ios - 使用 AVAudioEngine 为低延迟节拍器安排声音

swift - UIBezierPath roundedRect - 改变角的颜色

ios - MPMoviePlayerController 仅显示黑屏 - Swift