samedi 9 janvier 2016

Swift execute a simple test and pass the result inside a parameter of a function

I have found the below solution to execute a simple 'if ... then ... else' inside a parameter of a function. It is working but I'm quite sure that there is cleaner way to do that in Swift.

I was wondering if the use of closure could be a solution here, but the syntax and the logic of closure are not obvious to me yet. Before learning this part of the language, could someone tell me if it will indeed allow me to have a cleaner way of implementing this simple need ?

var tTexteSplitted: Array<String> = ["A", "B", "C"]

for (Index, Texte) in tTexteSplitted.enumerate()
{
    SomeFunction(Texte, TestValue((Index == 0), ReturnTrue: "SomeString", ReturnFalse: "AnotherString"))
}

func TestValue(Test: Bool, ReturnTrue: String, ReturnFalse: String) -> String
{
    if Test { return ReturnTrue } else { return ReturnFalse }
}

func SomeFunction(Param1: String, Param2: String)
{
    // Do something with Param1 and Param2
}

Aucun commentaire:

Enregistrer un commentaire