2017-09-19 5 views
-1

私はRxSwiftとMoyaを使用しています。私はtableViewControllerにラインauthors.map {...RxSwiftマッピング問題

fatal error: unexpectedly found nil while unwrapping an Optional value

エラーを取得しています。私がauthorsを印刷すると、それはゼロであることがわかりました。問題がどこにあるのかわかりませんでした。私は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) 
    } 
} 

私のViewModel:

//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

あなたもそこ)のViewModelと著者のプロパティに値を割り当てる、またはあなたがどこかにそれらを割り当てている場合、(setupBindingsを移動する必要があります。 P .: RxSwiftに関する質問ではありません

関連する問題