【问题标题】:What errors can happen when encode with Swift JSONEncoder使用 Swift JSONEncoder 编码时会发生什么错误
【发布时间】:2017-07-28 05:42:29
【问题描述】:

JSONEncoder 方法 func encode<T>(_ value: T) throws -> Data where T : Encodable 是可抛出的。

我想知道为什么它是可抛出的:如果要编码的值不符合Encodable,它不应该通过编译器,所以它应该不会在运行时发生错误。

【问题讨论】:

    标签: swift codable


    【解决方案1】:

    来自JSONEncodersource code

    /// - throws: `EncodingError.invalidValue` if a non-conforming floating-point value is encountered during encoding, and the encoding strategy is `.throw`.
    /// - throws: An error if any value throws an error during encoding.
    


    所有错误的调试说明:

    顶级(T.self)没有编码任何值。

    顶级 (T.self) 编码为空 JSON 片段。

    顶级 (T.self) 编码为数字 JSON 片段。

    顶级 (T.self) 编码为字符串 JSON 片段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2020-03-07
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 2012-01-03
      相关资源
      最近更新 更多