2017-09-19 14 views
-1

RxSwift와 Moya를 사용하고 있습니다. 나는 tableViewController에 라인 authors.map {...RxSwift 매핑 문제

fatal error: unexpectedly found nil while unwrapping an Optional value

오류를 얻고있다. 내가 authors를 인쇄 할 때 그것이 nil이라는 것을 알았습니다. 문제가있는 곳을 찾을 수 없었습니다. Moya-ObjectMapper/RxSwift 라이브러리를 사용하고 있습니다. 최신 릴리스 Observable + Objectmapper.swift 파일이 없으므로 github에서 발견하고 대체했습니다.

//import libraries 

class AuthorsTableViewController: UITableViewController { 

    var viewModel: AuthorsViewModelType! 
    var authors: Driver<RequestResult<[Author]>>! 
    private let disposeBag = DisposeBag() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     setUpBindings() 
    } 

    func setUpBindings() { 

     tableView.dataSource = nil 
     tableView.delegate = nil 

     authors.map { result -> [Author] in ---> Here 
      ... 
      }.disposed(by: disposeBag) 
    } 
} 

내 뷰 모델은 :

//import libraries 

public protocol AuthorsViewModelType { 
    func getAuthors(destination: YazarAPI) -> Driver<RequestResult<[Author]>> 
} 

public class AuthorsViewModel: AuthorsViewModelType { 
    fileprivate var provider = YazarAPI.sharedProviderInstance 

    public func getAuthors(destination: YazarAPI) -> Driver<RequestResult<[Author]>> { 
     return provider.request(destination) 
      .observeOn(MainScheduler.instance) 
      .filterSuccessfulStatusCodes() 
      .mapAuthors(destination: destination) 
      .map { .Success($0) } 
      .asDriver(onErrorRecover: { error in 
       return .just(.Error(ReqestError(description: error.localizedDescription, code: .requestError))) 
      }) 
    } 
} 

private extension Observable where E == Response { 
    func mapAuthors(destination: YazarAPI) -> Observable<[Author]> { 
     let authors: Observable<[Author]> 

     switch destination { 
     case .authors: 
      authors = self.mapObject(Authors.self).map { $0.authors ?? [] } 
     default: 
      fatalError("Unexpected request type \"\(destination)\"") 
     } 
     return authors 
    } 
} 

답변

0

당신은 뷰 모델과 저자 속성에 값을 할당하거나 당신이 어딘가에을 할당하는 경우도있다 setupBindings()를 이동해야합니다. 주 : RxSwift 질문이 아닙니다.