What Does the .2 in print(statistics.2) in This Code?

Hi. What does .2 mean in print(statistic.2) in this code:


func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {

var min = scores[0]

var max = scores[0]

var sum = 0


for score in scores {

if score > max {

max = score

} else if score < min {

min = score

}

sum += score

}


return (min, max, sum)

}

let statistics = calculateStatistics(scores: [5, 3, 100, 3, 9])

print(statistics.sum)

print(statistics.2)


Thank you in advance.

God bless, Proverbs 31

Accepted Answer

In Swift, `.n` (where n is a non-negative integer) represents the n-th member of a tuple.

So, in your case, `statistics.2` is equivalent to `statistics.sum`.

Thanks. I wish Swift would just standardized how they access items in collections:

sometimes it's the exact name like beatles.paul, for arrays its index [0], now here, it's I guess it's like a combination of an index and a name; and that indexes start with 1 (coz' that's more intuitive than starting with 0), so that Swift become even more safe programming language for the 21st century:-)


Though, thanks again.


Advanced Merry Christmas.

What Does the .2 in print(statistics.2) in This Code?
 
 
Q