0

import Foundation

struct locus { var x: Int var y: Int }

let aerodrome: [locus] = [(1,1), (2,2)]

produces error "Cannot convert value of type '(Int, Int)' to expected element type 'locus'" I love that the compiler accepts 'locus' as a type - I hate that it doesn't accept the Ints I try and input. Help appreciated.

EarthDemon
  • 13
  • 6

1 Answers1

1

Your literal [(1,1), (2,2)]is an array of tuples. Try:

let aerodrome = [(1,1), (2,2)]

in the playground and option-click aerodrome and you'll get:

let aerodrome: [(Int, Int)]

If you want an array of locus you need to create instances of them. One way to do this is to use the automatic default constructor which requires named arguments:

let aerodrome = [locus(x: 1,y: 1), locus(x: 2,y: 2)]

If you don't wish to use the labels write your own init.

CRD
  • 52,522
  • 5
  • 70
  • 86