Swift 3中的rac_textSignal

时间:2017-02-18 22:26:44

标签: ios swift swift3 signals reactive-cocoa

我正在尝试迁移我没有写入Swift 3的Swift 2.2应用程序。一切顺利,直到我遇到一些RAC调用。我不熟悉ReactiveCocoa,这使得这更具挑战性。

/**
 * Method name: Enable Login Button
 * Description: Enable or disable login button based on
 * username and password entry
 * Parameters: None
 */
func enableLoginButton() -> Void {
    self.btnLogin.isEnabled = false

    let serviceUrlSignal = self.txtServiceUrl.rac_textSignal()
        .toSignalAssumingHot()
        .assumeNoErrors()
        .map { text in text as! String }

    let usernameSignal = self.txtFieldUsername.rac_textSignal()
        .toSignalAssumingHot()
        .assumeNoErrors()
        .map { text in text as! String }

    let userNameCharSignal = usernameSignal.map { $0.characters.count }

    let passwordSignal = self.txtFieldPassword.rac_textSignal()
        .toSignalAssumingHot()
        .assumeNoErrors()
        .map { text in text as! String }

    let passwordCharSignal = passwordSignal.map { $0.characters.count }

    userNameCharSignal.combineLatestWith(passwordCharSignal)
        .map
        {
            charArray in charArray.0 > 0 && charArray.1 > 0
        }
        .observe { event in
            if let buttonEnabled = event.value {
                self.btnLogin.enabled = buttonEnabled
            }
    }

    viewModel.username <~ usernameSignal
    viewModel.password <~ passwordSignal
    viewModel.serviceUrl <~ serviceUrlSignal

    btnLogin.rac_signalForControlEvents(.TouchUpInside)
        .subscribeNext{
            button in
            print("Authenticate Click!")
    }
}

根据我的理解,ReactiveSwift中不存在rac_textSignal。我是否需要安装ReactiveObjC或者是否有可以用来替换此功能的Swift方法?谢谢。

1 个答案:

答案 0 :(得分:1)

除了ReactiveSwift之外,您还需要添加ReactiveCocoa。

自repo拆分以来,ReactiveCocoa的核心现在在ReactiveSwift中实现,但ReactiveCocoa添加了Cocoa特定的东西,比如你需要的UIKit绑定。

UIKit绑定可通过reactive属性访问,例如textfield.reactive.continuousTextValues

由于这些绑定已经正确输入(与rac_textSignal相对),您可以替换

的整个块。
let usernameSignal = self.txtFieldUsername.rac_textSignal()
    .toSignalAssumingHot()
    .assumeNoErrors()
    .map { text in text as! String }

...

viewModel.username <~ usernameSignal

viewModel.username <~ self.txtFieldUsername.reactive.continuousTextValues