Unfortunately we all know that we can’t put everything what we are working in, on a simple blog, I have to understand this since years ago when I worked at some government department. But I can show what I am doing now just for fun and for registry to me in the future.

we were had some problems here, especially when we’d need to try validate a lot of forms that involved things like UITextField, UISegmentedControl, etc. To solve this issue I had to create one class with methods like:

.insert(object, checkType, labelError, errorMessage) // magic goes here.
.showErrors() // print on screen all erros like below
.count() // return the quantity of erros below
.result() // return if all tests are passed or no.

and the code is easily readable:

@IBAction func onActAll(sender: AnyObject)
{
    if ruleThis.result() == false {
        ruleThis.showErrors()
    } else {
        print("Do your segueway or something after success.")
    }
}

You set a trigger for validation, here a button to Next Screen. Then you assign function pointers to the stack os Validations.

override func viewDidLoad() {
    super.viewDidLoad()

    ruleThis.insert(object: textFieldCPF,
        function: checkIfCPFisValid, errorLabel: labelMessageCPF,
        errorMessage: "CPF inválido, sua mensagem personalizada aqui.")

    ruleThis.insert(object: textFieldPassword,
        function: checkIfPasswordIsValid, errorLabel: labelMessagePassword,
        errorMessage: "Sua senha não preenche os resquisitos mínimos de segurança, tente novamente")

    ruleThis.insert(object: textFieldInputCEP,
        function: checkIfCEPisValid, errorLabel: labelMessageCEP,
        errorMessage: "Este CEP não é válido")

    ruleThis.insert(object: segmentedOption,
        function: checkIfIsSelected, errorLabel: labelMessageSegmented,
        errorMessage: "Por favor selecione uma das opções.")
}

Then you make a function validation to the object assigned. This coule be simple as

func checkIfTextIsBlank(obj: AnyObject?) -> Bool
{
    var result = false

    if let object = obj as? UITextField {
        if !object.text!.isEmpty { // if is not nil.

            result = true
        }
    }

    return result
}

or a little more complex like this (a validation check for social number in Brazil), validating screen in iOS with and the results are below:

// algoritmo: res1 = sum(digits[0-9] * 10...2) % 11; res2 = sum(digits[0-10] * 11...2) % 11

// if (digit10 == res1 && digit11 && res2) { cpf valido }

func checkIfCPFisValid(obj: AnyObject?) -> Bool
{
    var input: String = ""

    if let object = obj as? UITextField {
        input = object.text!
    }

    if (input.isEmpty) {
        return false
    }

    let cpf = input.characters.filter{"0123456789".characters.contains($0)}
    if cpf.count == 11 {
        let digitTen = (Int(String(cpf[9])) ?? 0)
        let digitEleven = (Int(String(cpf[10])) ?? 0)

        var resultModuleOne: Int = 0, resultModuleTwo: Int = 0, realValue: Int = 0
        var i: Int = 0, j: Int = 11
        for index in 0..<cpf.count-1 {
            realValue = (Int(String(cpf[index])) ?? 0)
            resultModuleTwo += (realValue * j)

            if (i > 0) {
                realValue = (Int(String(cpf[index-1])) ?? 0)
                resultModuleOne += (realValue * j)
            }

            i++; j--;
        }

        resultModuleOne %= 11
        resultModuleOne = resultModuleOne < 2 ? 0 : resultModuleOne-11

        resultModuleTwo %= 11
        resultModuleTwo = resultModuleTwo < 2 ? 0 : resultModuleTwo-11

        if (resultModuleOne == digitTen && resultModuleTwo == digitEleven) {
            return true
        }
    }

    return false
}

let cpf: String = "fdasfdfsdf352353"
let ret: Bool

ret = checkIfCPFisValid(cpf)
print ("\n\nret is '\(ret)' and cpf if '\(cpf)'")
if (!ret) {
    print ("CPF é inválido")
} else {
    print ("CPF é válido!! :-)")
}

](https://mulatinho.files.wordpress.com/2016/03/rulethis.gif)

For me the results are cool and work! I really enjoy work with Swift. :)