如何将零案例合并到失败案例 导入MVC 导入RxSwift 进口RxCocoa 导入RTVModel 导入RTVWebAPI 公共类设置ViewModel:ViewModel{ public var fetchedNotifications:Driver=.empty() 公共变量fetchedNotificationsFailed:驱动程序=.empty() public var notificationCount:Driver=.empty() ''''''''''''''' public var userLoginName:Driver=.empty() /////userLoginName getting是一个可选字符串。 ''''''''''''''''' 公共变量FetchedUserLoginName失败:驱动程序=.empty() public func bindNotificationEvents(带触发器:驱动程序){ 让Web服务:驱动程序=触发器 .map{RTVInformationListParameters()} .webService() let result=webService.request() notificationCount=result.success().map{$0.informationList.maxCount} fetchedNotifications=result.success() .map{$0.informationList.notifications} ------->.map{$0.map{NotificationItem.init(通知:$0)} /////////////////////////////////////////////////////////////// 错误(可选类型“字符串”的值必须解包为类型“字符串”的值) /////////////////////////////////////////////////////////////// fetchedNotificationsFailed=Driver.merge(fetchedNotificationsFailed,result.error().map{$0.message}) } public func bindUserInfoEvents(带触发器:驱动程序){ 让Web服务:驱动程序=触发器 .map{RTVMobileMenuParameters()} .webService() let result=webService.request() userLoginName=result.success().map{($0.mobileMenuInfo.username)} fetchedUserLoginNameFailed=Driver.merge(fetchedUserLoginNameFailed,result.error().map{$0.message}) } } 扩展RTVAPIError{ fileprivate var消息:字符串{ var message=“\(self.localizedDescription)” 如果let codeNumber=self.codeNumber{ 消息+=“\n(\(代码号))” } 回信 } }

如何将零案例合并到失败案例 导入MVC 导入RxSwift 进口RxCocoa 导入RTVModel 导入RTVWebAPI 公共类设置ViewModel:ViewModel{ public var fetchedNotifications:Driver=.empty() 公共变量fetchedNotificationsFailed:驱动程序=.empty() public var notificationCount:Driver=.empty() ''''''''''''''' public var userLoginName:Driver=.empty() /////userLoginName getting是一个可选字符串。 ''''''''''''''''' 公共变量FetchedUserLoginName失败:驱动程序=.empty() public func bindNotificationEvents(带触发器:驱动程序){ 让Web服务:驱动程序=触发器 .map{RTVInformationListParameters()} .webService() let result=webService.request() notificationCount=result.success().map{$0.informationList.maxCount} fetchedNotifications=result.success() .map{$0.informationList.notifications} ------->.map{$0.map{NotificationItem.init(通知:$0)} /////////////////////////////////////////////////////////////// 错误(可选类型“字符串”的值必须解包为类型“字符串”的值) /////////////////////////////////////////////////////////////// fetchedNotificationsFailed=Driver.merge(fetchedNotificationsFailed,result.error().map{$0.message}) } public func bindUserInfoEvents(带触发器:驱动程序){ 让Web服务:驱动程序=触发器 .map{RTVMobileMenuParameters()} .webService() let result=webService.request() userLoginName=result.success().map{($0.mobileMenuInfo.username)} fetchedUserLoginNameFailed=Driver.merge(fetchedUserLoginNameFailed,result.error().map{$0.message}) } } 扩展RTVAPIError{ fileprivate var消息:字符串{ var message=“\(self.localizedDescription)” 如果let codeNumber=self.codeNumber{ 消息+=“\n(\(代码号))” } 回信 } },swift,rx-swift,Swift,Rx Swift,这并不是您应该使用它的方式,因为驱动程序的要点不是出错,但您显然有一个错误状态,因此,可观察的或信号会更好 但是,您需要将信号分为成功信号和错误信号,如下所示: import MVVMC import RxSwift import RxCocoa import RTVModel import RTVWebAPI public class SettingsViewModel: ViewModel { public var fetchedNotifications: Driver<

这并不是您应该使用它的方式,因为
驱动程序的要点不是出错,但您显然有一个错误状态,因此,
可观察的
信号
会更好

但是,您需要将信号分为成功信号和错误信号,如下所示:

import MVVMC
import RxSwift
import RxCocoa
import RTVModel
import RTVWebAPI

public class SettingsViewModel: ViewModel {

    public var fetchedNotifications: Driver<[NotificationItem]> = .empty()

    public var fetchedNotificationsFailed: Driver<String> = .empty()

    public var notificationCount: Driver<Int> = .empty()
'''''''''''''''
    public var userLoginName: Driver<String> = .empty()
///// userLoginName getting is a optional String.

'''''''''''''''''

    public var fetchedUserLoginNameFailed: Driver<String> = .empty()

    public func bindNotificationEvents(with trigger: Driver<Void>) {

        let webService: Driver<RTVInformationListWebService> = trigger
            .map { RTVInformationListParameters() }
            .webService()

        let result = webService.request()

        notificationCount = result.success().map { $0.informationList.maxCount }
        fetchedNotifications = result.success()
            .map {$0.informationList.notifications}
------->    .map {$0.map {NotificationItem.init(notification: $0)}}
///////////////////////////////////////////////////////////////
Error (Value of optional type 'String?' must be unwrapped to a value of type 'String')
///////////////////////////////////////////////////////////////

        fetchedNotificationsFailed = Driver.merge(fetchedNotificationsFailed, result.error().map { $0.message })
    }

    public func bindUserInfoEvents(with trigger: Driver<Void>) {

        let webService: Driver<RTVMobileMenuWebService> = trigger
            .map { RTVMobileMenuParameters() }
            .webService()

        let result = webService.request()
        userLoginName = result.success().map { ($0.mobileMenuInfo.username) }
        fetchedUserLoginNameFailed = Driver.merge(fetchedUserLoginNameFailed, result.error().map { $0.message })

    }
}

    extension RTVAPIError {
        fileprivate var message: String {
            var message = "\(self.localizedDescription)"
            if let codeNumber = self.codeNumber {
                message += "\n(\(codeNumber))"
            }
            return message
        }
    }

我可能不懂语法,我是从内存中写的。

我用catchOnNil修复了它
.catchOnNil{return}

我想将nil案例合并到fetchUserLoginNameFailed驱动程序
fetchedNotifications = result.success()
    .map {$0.informationList.notifications}
    .share(replay: 1)

let success = fetchedNotifications
    .filter { $0 != nil }
    .map { $0.map { NotificationItem.init(notification: $0) } }

let error = fetchedNotifications
    .filter { $0 == nil } // Here would be your "error" state which you can merge later