使用swift 4可解码协议和RxSwift
我最近试图使用可解码协议将JSON解析为模型,并且我已成功完成该任务。但是现在我想用RxSwift实现双向绑定。为此,我需要声明类型的变量的变量<>“这里是我的模型片段:使用swift 4可解码协议和RxSwift
struct Person : Decodable {
var batchcomplete = String()
var `continue` = Continue()
var query = Query()
var limits = Limit()
enum CodingKeys: String,CodingKey
{
case batchcomplete
case `continue`
case limits
case query
}
init(from decoder: Decoder) throws
{
let container = try decoder.container(keyedBy: CodingKeys.self)
batchcomplete = try container.decode(String.self, forKey: .batchcomplete)
`continue` = try container.decode(Continue.self, forKey: .`continue`)
limits = try container.decode(Limit.self, forKey: .limits)
query = try container.decode(Query.self, forKey: .query)
}
}
现在,如果我从字符串()改变我的“batchcomplete”到变量时,init()方法引发错误:
No 'decode' candidates produce the expected contextual result type 'Variable<String>'.
进行这些更改,您将收到错误消息。
var batchcomplete = Variable<String>("") batchcomplete = try container.decode(Variable<String>.self, forKey: .batchcomplete)
回答:
不要试图解码为Variable
...只是将其值设置:
batchcomplete.value = try container.decode(String.self, forKey: .batchcomplete)
或者你也可以声明你的实例变量,并在你的初始化一次初始化它:
let batchcomplete: Variable<String> batchcomplete = Variable<String>(try container.decode(String.self, forKey: .batchcomplete))
在附注中,您的Variable
应该声明为常量(let),因为您更改了Variable
中包含的值。
以上是 使用swift 4可解码协议和RxSwift 的全部内容, 来源链接: utcz.com/qa/265670.html