RxSwift映射问题
我用的是RxSwift和Moya。我越来越 致命错误:在展开可选值时意外发现nil tableViewController中的RxSwift映射问题,swift,rx-swift,moya,Swift,Rx Swift,Moya,我用的是RxSwift和Moya。我越来越 致命错误:在展开可选值时意外发现nil tableViewController中的authors.map{…行出错。当我打印authors时,我发现它是零。我无法找到问题所在。我正在使用Moya ObjectMapper/RxSwift库。在最新版本中,Observable+ObjectMapper.swift文件丢失,因此我在github上找到并替换了它 //import libraries class AuthorsTableViewContro
authors.map{…
行出错。当我打印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)
}
}
//导入库
类AuthorsTableViewController:UITableViewController{
var viewModel:AuthorsViewModelType!
作者:司机!
私有出租dispebag=dispebag()
重写func viewDidLoad(){
super.viewDidLoad()
setUpBindings()
}
func setUpBindings(){
tableView.dataSource=nil
tableView.delegate=nil
authors.map{result->[Author]在--->这里
...
}.处置(由:处置人)
}
}
我的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
}
}
//导入库
公共协议作者ViewModelType{
func getAuthors(目的地:YazarAPI)->驱动程序
}
公共类AuthorsViewModel:AuthorsViewModelType{
fileprivate var provider=YazarAPI.sharedProviderInstance
public func getAuthors(目的地:YazarAPI)->驱动程序{
返回提供程序。请求(目标)
.observeOn(MainScheduler.instance)
.filterSuccessfulStatusCodes()
.mapAuthors(目的地:目的地)
.map{.Success($0)}
.asDriver(onErrorRecover:{中出现错误
return.just(.Error(requestError(description:Error.localizedDescription,code:.requestError)))
})
}
}
可观测的私有扩展,其中E==响应{
func地图作者(目的地:YazarAPI)->可观察{
让作者:可观察
交换机目的地{
案例.作者:
authors=self.mapObject(authors.self).map{$0.authors???[]}
违约:
fatalError(“意外请求类型\”(目标)\“”)
}
返回作者
}
}
您应该为viewModel和authors属性指定值,或者如果要在某个位置指定它们,也可以将setupBindings()移动到该位置。
附言:这不是RxSwift的问题