Apollo graphQL:在 iOS swift 中使用自定义标量时解码数组时出错
Apollo graphQL: Error decoding Array while using custom scalars in iOS swift
我的架构如下所示。这里 question_r 是接收为 Json 的字典数组。我在转换它时遇到问题。有什么方法可以成功解码吗?
"questionR": [
{
"identifier": "123",
"skills": {
"primary_skill": "vocabulary",
"secondary_skill": "reading",
}
}
]
这是架构
{
"name": "question_r",
"description": "",
"args": [],
"type": {
"kind": "SCALAR",
"name": "Json",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
}
我的脚本是这样的:
SCRIPT_PATH="${PODS_ROOT}/Apollo/scripts"
CD "${SRCROOT}/${TARGET_NAME}"
"${SCRIPT_PATH}"/运行-bundled-codegen.sh codegen:generate --target=swift --includes=./**/*.graphql --passthroughCustomScalars --localSchemaFile="schema.json" API.swift
这是我定义的类型别名Json。它可以是字典或字典数组
public typealias Json = [String:Any?]
extension Dictionary: JSONDecodable {
public init(jsonValue value: JSONValue) throws {
guard let dictionary = value as? Dictionary else {
throw JSONDecodingError.couldNotConvert(value: value, to: Dictionary.self)
}
self = dictionary
}
}
extension Array: JSONDecodable{
public init(jsonValue value: JSONValue) throws {
guard let array = value as? Array else {throw JSONDecodingError.couldNotConvert(value: value, to: Array.self)}
self = array
}
}
有什么解决方法吗?我在这里遗漏了什么吗?
我通过假设 Json 是一个数组并将字典转换为数组来让它工作。
public typealias Json = [[String:Any?]]
extension Json: JSONDecodable{
public init(jsonValue value: JSONValue) throws{
guard let array = value as? Array else {
guard let dict = value as? Dictionary<String, Any> else { throw JSONDecodingError.couldNotConvert(value: value, to: Dictionary<String, Any>.self)
}
self = .init(arrayLiteral: dict)
return
}
self = array
}
}
这对我有用。
我的架构如下所示。这里 question_r 是接收为 Json 的字典数组。我在转换它时遇到问题。有什么方法可以成功解码吗?
"questionR": [
{
"identifier": "123",
"skills": {
"primary_skill": "vocabulary",
"secondary_skill": "reading",
}
}
]
这是架构
{
"name": "question_r",
"description": "",
"args": [],
"type": {
"kind": "SCALAR",
"name": "Json",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
}
我的脚本是这样的:
SCRIPT_PATH="${PODS_ROOT}/Apollo/scripts" CD "${SRCROOT}/${TARGET_NAME}" "${SCRIPT_PATH}"/运行-bundled-codegen.sh codegen:generate --target=swift --includes=./**/*.graphql --passthroughCustomScalars --localSchemaFile="schema.json" API.swift
这是我定义的类型别名Json。它可以是字典或字典数组
public typealias Json = [String:Any?]
extension Dictionary: JSONDecodable {
public init(jsonValue value: JSONValue) throws {
guard let dictionary = value as? Dictionary else {
throw JSONDecodingError.couldNotConvert(value: value, to: Dictionary.self)
}
self = dictionary
}
}
extension Array: JSONDecodable{
public init(jsonValue value: JSONValue) throws {
guard let array = value as? Array else {throw JSONDecodingError.couldNotConvert(value: value, to: Array.self)}
self = array
}
}
有什么解决方法吗?我在这里遗漏了什么吗?
我通过假设 Json 是一个数组并将字典转换为数组来让它工作。
public typealias Json = [[String:Any?]]
extension Json: JSONDecodable{
public init(jsonValue value: JSONValue) throws{
guard let array = value as? Array else {
guard let dict = value as? Dictionary<String, Any> else { throw JSONDecodingError.couldNotConvert(value: value, to: Dictionary<String, Any>.self)
}
self = .init(arrayLiteral: dict)
return
}
self = array
}
}
这对我有用。