Search code examples
swiftreactive-cocoa

swift ReactiveCocoa combineLatest


in obj-c, I can use this method:

RACSignal *signUpActiveSignal = [RACSignal combineLatest:@[validUsernameSignal, validPasswordSignal]
                reduce:^id(NSNumber*usernameValid, NSNumber *passwordValid){
                  return @([usernameValid boolValue]&&[passwordValid boolValue]);
                }];

when i translate it to swift, like this:

RACSignal.combineLatest([accountSignal, passwordSignal]) { () -> AnyObject! in
        // arguments
        return true
        }.subscribeNext { (reduceResult: AnyObject!) -> Void in
        KMLog("\(reduceResult)")
    }

how can i get the parameters


Solution

  • I haven't been able to figure out how to use that closure for a combineLatest, but you can use map. The input is an RACTuple object. You can get the objects from the input signals like this:

    RACSignal.combineLatest([accountSignal, passwordSignal]).map {
            let tuple = $0 as! RACTuple
            let account = tuple.first as! String
            let password = tuple.second as! String
            // your code here
        }
    

    Obviously you'll want to cast your objects to their actual types, but I just used Strings here as an example. And remember that you'll have to return the combined object at the end of the map closure.