I practice tuples mini-exercises in Chapter 3 about types and operations.
I don’t think that it should behave like that but if I make a mistake somewhere please correct me.
When I declare a tuple like so:
let dateWithTemperature1: (Int, Int, Int, Double) = (day: 17, month: 2, year: 2017, averageTemp: 7.0)
and I try to get access to items of dateWithTemperature1 tuple I can’t refer to them by their names.
When I declare a tuple with types set by type inference:
let dateWithTemperature2 = (day: 17, month: 2, year: 2017, averageTemp: 7.0)
I can easily get access to data by numbers and by names.
What do you think of that guys?
In order for the above code to work properly, this is how you should declare the tuple in this case:
let dateWithTemperature1: (day: Int, month: Int, year: Int, averageTemp: Double) = (17, 2, 2017, 7.0)
As you can see, each and every tuple label appear next to its corresponding and associated tuple type in the tuple’s definition, declaration and prototype. Now you can access the tuple’s values as you would normally do with type inference after all for sure and for good indeed:
dateWithTemperature1.day
dateWithTemperature1.month
dateWithTemperature1.year
dateWithTemperature1.averageTemp
Please let me know if you have any questions regarding the whole thing. Thanks! :]
2 Likes
Thanks, i write two solution for this exercise:
let dayWithTemperature: (month:Int , day:Int, year:Int , temperature:Double) = (17,10,1993,25.7)
dayWithTemperature.day
dayWithTemperature.month
dayWithTemperature.temperature
dayWithTemperature.year
let giornoConTemperatura = (mese:17,giorno:10,anno:19,temperatura:106.6)
giornoConTemperatura.mese
giornoConTemperatura.giorno
giornoConTemperatura.anno
giornoConTemperatura.temperatura
@osvimong Thank you for sharing your solution - much appreciated! :]