2017-12-27 33 views
0

최근에 디코딩 가능 프로토콜을 사용하여 JSON을 모델로 구문 분석하려고했지만 성공적으로 완료했습니다. 하지만 이제는 RxSwift를 사용하여 양방향 바인딩을 구현하려고합니다. . 내가 변수에 문자열()에서 초기화를 내 '으로 BatchComplete'을 변경하는 경우 이제신속한 사용 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) 
    } 
} 

() 메소드 : 그 동안 나는 '변수 <>'여기 내 모델에서 코드 조각입니다 유형의 변수를 선언해야 오류 :

No 'decode' candidates produce the expected contextual result type 'Variable<String>'. 

이러한 변경을하면 오류가 발생합니다.

var batchcomplete = Variable<String>("") 
batchcomplete = try container.decode(Variable<String>.self, forKey: .batchcomplete) 

답변

2

그냥 그 값으로 설정 ... 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에 포함 된 값을 변경하기 때문에 Variable은 상수 (let)로 선언해야합니다.

+0

고마워요 .-) – Reckoner