ios - Why to use tuples when we can use array to return multiple values in swift -


today going through basic swift concepts , working examples understand concepts. right have completed studying tuples.

i have got 1 doubt i.e, need of using tuples ? ya did digging on here got :

we can able return multiple values function. ok can returning array.

array ok can return multiple values of different types. ok cool can done array of anyobject :

    func calculatestatistics (scores:[int])->[anyobject] {     var min = scores[0]     var max = scores[0]     var sum = 0      score in scores     {         if score > max{             max = score         }         else if score < min{             min = score         }         sum += score     }     return [min,max,"hello"] }  let statistics = calculatestatistics([25,39,78,66,74,80])  var min = statistics[0] var max = statistics[1] var msg = statistics[2] // contains hello 

we can name objects present in tuples. ok can use dictionary of anyobject.

i not saying why use tuples when have got . there should tuple can able or easy tuples. people created swift wouldn't have involved tuples in swift if there wasn't reason. there should have been reason them involve it.

so guys please let me know if there's specific cases tuples best bet.

thanks in advance.

tuples anonymous structs can used in many ways, , 1 of them make returning multiple values function easier.

the advantages of using tuple instead of array are:

  • multiple types can stored in tuple, whereas in array restricted 1 type (unless use [anyobject])
  • fixed number of values: cannot pass less or more parameters expected, whereas in array can put number of arguments
  • strongly typed: if parameters of different types passed in wrong positions, compiler detect that, whereas using array won't happen
  • refactoring: if number of parameters, or type, change, compiler produce relevant compilation error, whereas arrays pass unnoticed
  • named: it's possible associate name each parameter
  • assignment easier , more flexible - example, return value can assigned tuple:

    let tuple = functionreturningtuple() 

    or parameters can automatically extracted , assigned variables

    let (param1, param2, param3) = functionreturningtuple() 

    and it's possible ignore values

    let (param1, _, _) = functionreturningtuple() 
  • similarity function parameters: when function called, parameters pass tuple. example:

    // swift 2 func dosomething(number: int, text: string) {     println("\(number): \(text)") }  dosomething(1, "one")  // swift 3     func dosomething(number: int, text: string) {     print("\(number): \(text)") }  dosomething(number: 1, text: "one") 

    (deprecated in swift 2) function can invoked as:

    let params = (1, "one") dosomething(params) 

this list not exhaustive, think there's enough make favor tuples arrays returning multiple values


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -