0

我正在使用 ReactiveCocoa4 为我的项目添加一些基本的登录功能。我在 viewModel 中设置了“用户名”和“密码”MutableProperties,并将它们绑定到 viewController 中的关联文本字段。到目前为止一切顺利,但我坚持设置动作来执行网络请求。本质上,我希望操作采用输入元组(用户名:字符串,密码:字符串)并输出我的自定义用户对象“SBUser”。我还在对用户名和密码输入进行一些基本验证,并将其与操作的启用状态联系起来。我的 viewModel 代码如下。

final class AuthenticationVM {

let client: Client
let authenticationType: AuthenticationType
let username = MutableProperty<String>("")
let password = MutableProperty<String>("")
let loginAction: Action<(username: String, password: String), SBUser, Error>

init(client: Client, authenticationType: AuthenticationType) {
    self.client = client
    self.authenticationType = authenticationType

    let validation = combineLatest(username.producer, password.producer)
            .map({ (username, password) -> Bool in
                return username.characters.count > 2 && password.characters.count > 2 })
            .skipRepeats()

    loginAction = Action<(username: String, password: String), SBUser, Error>(enabledIf: validation) { (username: String, password: String) in
        return SignalProducer<SBUser, Error> { [unowned self] observer, _ in
            self.client.request(API.logInWithUsername(username, password: password)) { response in
                        switch response.result {
                        case .Success(let user):
                            observer.sendNext(user)
                        case .Failure(let error):
                            observer.sendFailed(error)
                        }
            }
        }
    }
}

}

我收到一个错误,上下文闭包类型 ' ->SignalProducer< ,_>' 需要 1 个参数,但在闭包主体中使用了 2 个参数。不能将这样的元组传递给动作的输入吗?

4

2 回答 2

1
loginAction = Action<(username: String, password: String), SBUser, Error>(enabledIf: MutableProperty(validation)) { (username: String, password: String) in
        return SignalProducer<SBUser, Error> { [unowned self] observer, _ in
            self.client.request(API.logInWithUsername(username, password: password)) { response in
                switch response.result {
                case .Success(let user):
                    observer.sendNext(user)
                case .Failure(let error):
                    observer.sendFailed(error)
                }
            }
        }
    }

对可变属性的类型转换验证然后它将起作用enabledIf: MutableProperty(validation)

于 2016-03-21T06:04:34.133 回答
0
public init<P : PropertyType where P.Value == Bool>(enabledIf: P, _ execute: Input -> ReactiveCocoa.SignalProducer<Output, Error>)

enabledIf必须是 PropertyType。

于 2016-02-24T01:12:05.270 回答