Json 在Swift中解码一个可编码的
很难做到这一点:我试图将JSON解码抽象为一个函数,将一个可编码的参数加上一些数据作为参数 因此,如果可能的话,我需要有以下函数签名:Json 在Swift中解码一个可编码的,json,swift,struct,codable,decoder,Json,Swift,Struct,Codable,Decoder,很难做到这一点:我试图将JSON解码抽象为一个函数,将一个可编码的参数加上一些数据作为参数 因此,如果可能的话,我需要有以下函数签名: func-doTheJSONDecoding(cdbl:Codable,data:data){…} 这是我的代码,从数据模型开始。下面有两个例子 import UIKit import Foundation struct MyStructCodable : Codable { let items : [MyValue]? } struct MyVal
func-doTheJSONDecoding(cdbl:Codable,data:data){…}
这是我的代码,从数据模型开始。下面有两个例子
import UIKit
import Foundation
struct MyStructCodable : Codable {
let items : [MyValue]?
}
struct MyValue : Codable {
let value : String?
}
let dta: Data = """
{
"items": [
{
"value": "Hello1"
}
]
}
""".data(using: .utf8)!
然后是两个例子:
// Example 1: this code works fine !!!!!!!!!!!!!!!!!!!!!!!!
let decoder = JSONDecoder()
do {
let result = try decoder.decode(MyStructCodable.self, from: dta)
print(result.items?[0].value ?? "")
} catch {
print(error)
}
// above code prints: Hello1
// Example 2: this code does not work - WHY ???????????????
func doTheJSONDecoding(cdbl: Codable, data: Data) {
let decoder = JSONDecoder()
do {
let result = try decoder.decode(cdbl, from: data)
print(result.items?[0].value ?? "")
} catch {
print(error)
}
}
let myValue = MyValue(value: "Hello2")
let myStructyCodable = MyStructCodable(items: [myValue])
doTheJSONEncoding(cdbl: myStructyCodable, data: dta)
抛出的错误在函数内部,它表示:
是否有任何方法可以保留函数签名(即
func dothejsondeconding(cdbl:Codable,data:data))
并且仍然可以使用它??非常感谢您的帮助。下面是我试图让您的func工作的尝试,它可能会有所改进,但它确实返回一个正确解码的对象。请注意,它采用的是对象的类型,而不是对象,实现可解码的是类型T
func doTheJSONEncoding<T: Decodable>(cdbl: T.Type, data: Data) -> T? {
let decoder = JSONDecoder()
do {
let result = try decoder.decode(cdbl.self, from: data)
return result
} catch {
print(error)
}
return nil
}
//testing it
let myValue = MyValue(value: "Hello2")
let myStructyCodable = MyStructCodable(items: [myValue])
let decoded = doTheJSONEncoding(cdbl: MyStructCodable.self, data: dta)
print(decoded?.items?[0].value ?? "")
func-doTheJSONEncoding(cdbl:T.Type,data:data)->T{
let decoder=JSONDecoder()
做{
let result=try decoder.decode(cdbl.self,from:data)
返回结果
}抓住{
打印(错误)
}
归零
}
//测试它
让myValue=myValue(值:“Hello2”)
让myStructyCodable=MyStructCodable(项:[myValue])
let decoded=doTheJSONEncoding(cdbl:MyStructCodable.self,data:dta)
打印(已解码?.items?[0]。值??“”)
这是我尝试让func工作的地方,它可能会有所改进,但它确实返回一个正确解码的对象。请注意,它采用的是对象的类型而不是对象,而实现可解码的是类型T
func doTheJSONEncoding<T: Decodable>(cdbl: T.Type, data: Data) -> T? {
let decoder = JSONDecoder()
do {
let result = try decoder.decode(cdbl.self, from: data)
return result
} catch {
print(error)
}
return nil
}
//testing it
let myValue = MyValue(value: "Hello2")
let myStructyCodable = MyStructCodable(items: [myValue])
let decoded = doTheJSONEncoding(cdbl: MyStructCodable.self, data: dta)
print(decoded?.items?[0].value ?? "")
func-doTheJSONEncoding(cdbl:T.Type,data:data)->T{
let decoder=JSONDecoder()
做{
let result=try decoder.decode(cdbl.self,from:data)
返回结果
}抓住{
打印(错误)
}
归零
}
//测试它
让myValue=myValue(值:“Hello2”)
让myStructyCodable=MyStructCodable(项:[myValue])
let decoded=doTheJSONEncoding(cdbl:MyStructCodable.self,data:dta)
打印(已解码?.items?[0]。值??“”)
好的,我在示例中进行了说明,并将函数重命名为dothejsondeconding()
。-我显然希望解码(而不是编码)-因此我认为我需要保留jsondecorder。(即,我确实有JSON数据,我希望将其解码为结构)。此外,将函数签名更改为dothejsondeconding()(电缆:可解码,数据:数据)
也没有帮助…)cdbl
必须是一个具体类型,而不是协议。你可以使用协议作为泛型的约束。并且任何符合Codable
的类型都没有items
@vadian:谢谢,啊,我明白了,类型是必需的,而不是一致性。因此,我想,没有办法抽象出任何符合可解码的类型(或者一般来说是可编码的)-我说得对吗?如果没有-我还怎么做?好的,我在示例中进行了澄清,并将函数重命名为dothejsondeconding()
。-我显然想解码(而不是编码)-因此我想我需要保留jsondecorder。(也就是说,我有JSON数据,我想将其解码为一个结构)。此外,将函数签名更改为dothejsondeconding(电缆:可解码,数据:数据)
也没有帮助…)cdbl
必须是一个具体类型,而不是协议。你可以使用协议作为泛型的约束。并且任何符合Codable
的类型都没有items
@vadian:谢谢,啊,我明白了,类型是必需的,而不是一致性。因此,我想,没有办法抽象出任何符合可解码的类型(或一般来说是可编码的)-我说得对吗?如果没有-我怎么做?非常感谢你,Joakim。这看起来不错-我会试试。你只需要
,不需要编码。非常感谢你,Joakim。这看起来不错-我会试试。你只需要
,不需要编码。