Home>

The first time I learned about tuples in swift.
I know how to use it, but I don't clearly understand the advantages and disadvantages.
What are the pros and cons?

  • Answer # 1

    (Hereafter, the answer will be corrected after receiving fuzzball's advice)

    Compare tuples and arrays to see the merits and demerits.

    gives a little explanation of the special Any type first.
    There are various types in Swift, but there is a special type called Any type. Variables and constants declared with this Any type can now contain any type of value. However, there is a danger that being able to enter any value is dangerous and it is not easy to understand what the contents are. So this is also possible.

    var anyTypeValue: Any = 1
    anyTypeValue = "String"
    anyTypeValue = 10.5

    There is also an array using this Any type

    let anyTypeArray: [Any] = [1, "string", 100000.5]

    You can create arrays with various types of values ​​like this. The values ​​in this array are allvalues ​​of Any type, so I don't know what type they are actually. If you don't know what type the array contains, it's difficult to handle this array.
    (Revised after receiving fuzzball's comment)
    When the contents of anyTypeArray are extracted as subscripts, anyTypeArray [0] is Int type, anyTypeArray [1] is String type, and anyTypeArray [2] is Double type value. However, if you do not know how many types until you extract the contents of anyTypeArray, it leads to the disadvantage that it is difficult to handle this array.

    On the other hand for tuples

    let tuple = (100, "hoge", 5.5)

    Tuples with various types of values ​​can be easily created. Here we know the type of the value in the tuple, and in the above example it will be of type Int, String, Double.
    If you quote fuzzball's comment here

      

    -Tuples are easy to "store multiple values ​​with different types"

    You can say

    . This is the merit of tuples.

    Although arrays can be manipulated, tuples cannot.
    An array operation is something that performs some operation on the contents of an array, such as.enumarated,.map,.foreach. For details, if you search by array operation, it will come out variously, so I will omit it.

    The following is an example.

    stringArray.map {print ($0)} // OK This $0 represents the contents of each array of stringArray.
    tuple.map {print ($0)} // NG

    A number of array operations can be performed in succession, and there is no need to put the result of each operation in a constant or variable. This is a great advantage that arrays are arrays.

    Tupples and arrays are similar in that both can be extracted using numbers (although it is a miscellaneous expression). I will pull out the above two examples again

    let anyTypeArray: [Any] = [1, "string", 100000.5]
    let tuple = (100, "hoge", 5.5)
    print (anyTypeArray [0] // 1
    print (tuple.0) // 100

    You can take it out like this.

    To enjoy the benefits of both

    let tupleArray: [(Int, String)] = [(0, "Aiueo"), (1, "Kakikueko")]

    There are cases where you want to create a specific tuple array (in this case, someArray.enumarated () is better, but this is for ease of understanding).