1

I have been following reactive swift recently to realise it's functionality. This is the playground info that is currently available.

https://github.com/ReactiveCocoa/ReactiveSwift/blob/master/ReactiveSwift-UIExamples.playground/Pages/ValidatingProperty.xcplaygroundpage/Contents.swift

What i am facing the problem is how to connect the view model with the UI element in order to monitor their values.

What I have tried is to emulate a simple login screen with two textfields and a button. What I aim to do is monitor the i/p from these two textfields and the button state will be changed based on that.

This is the view model

import Foundation
import ReactiveSwift

class LoginViewModel {

    /*
     * A property, represented by the PropertyProtocol, stores a value and notifies observers about future changes to that value. A validating protocol is nothing but a property that needs to be validated ( the validation is a lambda function)
     */
    let email: ValidatingProperty<String, Error>
    let password : ValidatingProperty<String,Error>

    init() {
        email =  ValidatingProperty("") { input in

            if (input.isEmpty == true || (input.isEmpty && input.isValidEmail() == false)){

                return .invalid(CustomError.init(title: nil, description: "Improper Email", code:  001))
            }
            return .valid
        }

        password = ValidatingProperty("") { input in

            if (input.isEmpty == true || (input.isEmpty && input.isValidPassword() == false)){

                 return .invalid(CustomError.init(title: nil, description: "Kindly please provide a password", code:  002))
            }else {
                return .valid
            }

        }
    }

}

This is the ViewController

class ViewController: UIViewController {
    @IBOutlet weak var emailField: UITextField! // Observable. Signal
    @IBOutlet weak var passwordField: UITextField! // Observable. Signal

    @IBOutlet weak var submitBtn: UIButton! 

    var viewModel: LoginViewModel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        viewModel = LoginViewModel()

    }
}

When I try to follow the playground's code {code} viewModel.email <~ formView.emailField.reactive .continuousTextValues.skipNil(){code} into my Sample code, it gives me an error stating that Value of type 'UITextField?' has no member 'reactive'. I added the helper functions available, emailField doesn't have a category for reactive, it's the other way around.

1 Answers1

0

I found that to do the UI binding, the project needed ReactiveCocoa pod library.