arquivo

Arquivo da tag: swift

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), and the results are below:

validating screen in iOS with Swift

validating screen in iOS with Swift

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

Some days ago I began to work with Swift, the new programming language of Apple, and iOS! Some people even said that I betrayed the movement! LOL! Really, I feel a little sad to let the open source projects in background for a time but for other side I am really motivated to work with new things and learn new technologies.

Swift is a mix of the best of programming languages, she have tuples, dictionaries, nested functions and little new concepts like optionals! I am really enjoying learning a lot with my new tech friends and really liking the new spirit of innovation that surrounds here.

Some basic tips of Swift:

– Variables are generic but you also can put his value type.

– You need ‘let’ or ‘var’ to declare one variable.
let something: String = “This is a immutable string”
var somethingOther = “This is a muttable string”

– Enums can be almost anything not just only numbers.

– Cases in switch can do amazing things like compare if value is some value type or another.

– Default parameters in functions are awesome like in Python!

– Doing for-loops are very fun!
for i in 1..<someVariable.count { makeWhatYouNeed() }

– Lazy properties are crazy and useful
private var someNasty: String
var usefulThing:String { get { return someNasty } set { someNasty = usefulThing } }

It’s easy create some fun to a nerdy guy! For the first time in my life, I have to thank to Apple for this!

%d blogueiros gostam disto: