【问题标题】:How to set a bool value to drive<Bool>如何设置一个布尔值来驱动<Bool>
【发布时间】:2019-12-18 09:06:05
【问题描述】:
var readIndicatorNeedsDisplay: Driver<Bool> = .empty()
public func bindcellEvents(readNotificationID: String) {
        if let unreadNotificationIDs = UserDefaults.main?.unreadNotificationIDs, unreadNotificationIDs.contains(readNotificationID) {
            readIndicatorNeedsDisplay = true

        } else {
            UserDefaults.main?.unreadNotificationIDs.append(readNotificationID)
            readIndicatorNeedsDisplay = false

// Cannot assign value of type 'Bool' to type 'Driver<Bool>' (aka 'SharedSequence<DriverSharingStrategy, Bool>')

        }
    }

当我将 bool 分配给驱动程序时出现错误:无法将类型“Bool”的值分配给类型“驱动程序”(又名“SharedSequence”)

【问题讨论】:

  • 请按照说明在 StackOverflow 上提问。

标签: rx-swift rx-cocoa


【解决方案1】:

您不应该将值直接分配给驱动程序,并且尝试这样做表明您对 RxSwift 的根本误解。您可能应该退后一步,学习基础知识,然后再回到这个问题。

但是,如果您想将值提供给流,您可以使用 PublishRelay

var readIndicatorNeedsDisplay = PublishRelay<Bool>()

public func bindcellEvents(readNotificationID: String) {
    if let unreadNotificationIDs = UserDefaults.main?.unreadNotificationIDs, unreadNotificationIDs.contains(readNotificationID) {
        readIndicatorNeedsDisplay.accept(true)

    } else {
        UserDefaults.main?.unreadNotificationIDs.append(readNotificationID)
        readIndicatorNeedsDisplay.accept(false)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多