I have the following code
let moveDirections = ["East": CGPoint(x: 1.0, y: 0.0),
"North": CGPoint(x: 0.0, y: 1.0),
"South": CGPoint(x: 0.0, y: -1.0),
"West": CGPoint(x: -1.0, y: 0.0),
]
let direction = "South"
let cdx = moveDirections[direction]?.x
Why does it insert the ? Surely the moveDirections variable contents is not optional.
-
—
MrMajorThorburn
Add a CommentI have worked round this using the following code
let moveDirections: [(Double,Double)] = [(1.0, 0.0), // East (0.0, 1.0), // North (1.0, 1.0), // NorthEast (-1.0, 0.0), // NorthWest (0.0, -1.0), // South (1.0, -1.0), // SouthEast (-1.0, -1.0), // SouthWest (-1.0, 0.0) // West ] var directions = ["East","North","NorthEast","NorthWest","South","SouthEast","SouthWest","West"] let id = directions.firstIndex(of: direction) let dt = moveDirections[id!] let dx = dt.0 let dy = dt.1