JSONDecoder 无法解码 *value/fragment* 上的类型 *Mac*
JSONDecoder unable to decode *value/fragment* types on *Mac*
有谁知道配置 JSONDecoder
Mac 版本的方法,以便它能够 decode
fragment/value类型?
我正在编写 Swift 需要在 Mac 和 iOS 上工作的代码。我正在使用 Decodable
和 JSONDecoder
来尝试解析一些 JSON。我有一个枚举 Decodable
如下:
enum SomeEnum: Int, Decodable {
case valueA = 0
case valueB = 1
}
为了测试 Decodable
方面,我进行了单元测试...
func test_problem() {
let jsonData = "0".data(using: .utf8)!
let result = try! JSONDecoder().decode(SomeEnum.self, from: jsonData)
XCTAssertEqual(result, SomeEnum.valueA)
}
当 运行 攻击 iOS 目标时,这会成功。但是,当 运行 反对 "My Mac"(在 Mojave 上)时,decode
调用会抛出错误,如下所示:
Thread 1: Fatal error: 'try!' expression unexpectedly raised an error:
Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath:
[], debugDescription: "The given data was not valid JSON.",
underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840
"JSON text did not start with array or object and option to allow
fragments not set." UserInfo={NSDebugDescription=JSON text did not
start with array or object and option to allow fragments not set.})))
似乎 iOS JSONDecoder
能够解码 fragments/value 类型,但 JSONDecoder
的 Mac 版本不能。
在莫哈韦沙漠(以及 iOS 12)和更低的地方你不能。
Mac JSONDecoder
可以解码 Catalina 10.15.1 及更高版本中的片段
有谁知道配置 JSONDecoder
Mac 版本的方法,以便它能够 decode
fragment/value类型?
我正在编写 Swift 需要在 Mac 和 iOS 上工作的代码。我正在使用 Decodable
和 JSONDecoder
来尝试解析一些 JSON。我有一个枚举 Decodable
如下:
enum SomeEnum: Int, Decodable {
case valueA = 0
case valueB = 1
}
为了测试 Decodable
方面,我进行了单元测试...
func test_problem() {
let jsonData = "0".data(using: .utf8)!
let result = try! JSONDecoder().decode(SomeEnum.self, from: jsonData)
XCTAssertEqual(result, SomeEnum.valueA)
}
当 运行 攻击 iOS 目标时,这会成功。但是,当 运行 反对 "My Mac"(在 Mojave 上)时,decode
调用会抛出错误,如下所示:
Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.})))
似乎 iOS JSONDecoder
能够解码 fragments/value 类型,但 JSONDecoder
的 Mac 版本不能。
在莫哈韦沙漠(以及 iOS 12)和更低的地方你不能。
Mac JSONDecoder
可以解码 Catalina 10.15.1 及更高版本中的片段