Tuple not corresponding to components?

Hi,

I was trying to do this --

let test: (Int, Int, Int) = (day1: 15, month1: 12, year1: 2018)

let x = test.day1

but I keep getting error - Value of test type '(Int, Int, Int)' has no member 'day1'

kindly help

Labeled tuples are types of their own. In your case (Int, Int, Int) is a super type of (day1: Int, month1: Int, year1: Int). In other words you wrote something similar to let test: NSObject = UIView().

You have multiple options now:

  1. Remove (Int, Int, Int).
  2. Rename it to (day1: Int, month1: Int, year1: Int).
2 Likes

When an element of a tuple type has a name, that name is part of the type. See the Language Reference.

So this will work:

let test: (day1: Int, month1: Int, year1: Int) = (day1: 15, month1: 12, year1: 2018)

let x = test.day1

Or simply let type inference do its work:

let test = (day1: 15, month1: 12, year1: 2018)

let x = test.day1
3 Likes

@DevAndArtist @dennisvennink -Thanks, but I am very confused here , so earlier I was able to do this -

let test(Int, Int) = (2, 3)

but the moment I add the names to their component I get error

let test(Int, Int) = (row: 2, column: 4)

test.row --> error    

Is the language rule that the moment I add the data types to tuple declaration, I cannot use the dot operator to access its components with names, and what is the logic for this ? Kindly guide

let tuple_1: (Int, Int) = (row: 2, column: 4)
let tuple_2: (row: Int, column: Int) = (row: 2, column: 4)
let tuple_3: (row: Int, column: Int) = (2, 4)
let tuple_4 = (row: 2, column: 4) // inferred type will be `(row: Int, column: Int)`

type(of: tuple_1) == type(of: tuple_2) // false
type(of: tuple_2) == type(of: tuple_3) // true

tuple_1.0 // the only way to get `row`
tuple_1.1 // the only way to get `column`

tuple_2.row // okay here
tuple_2.column // okay here
tuple_2.0 // also `row`
tuple_2.1 // also `column`

// tuple_3 and tuple_4 are the same as tuple_2
7 Likes