How to create a dictionary with key type point (x, y) and value type string in Swift

1 Answer

0 votes
import Foundation

// Define Point as a struct that conforms to Hashable
struct Point: Hashable {
    let x: Int
    let y: Int
}

// Create a dictionary with Point keys and String values
let pointDict: [Point: String] = [
    Point(x: 2, y: 7): "A",
    Point(x: 3, y: 6): "B",
    Point(x: 0, y: 0): "C"
]

// Print x and y separately
for (point, value) in pointDict {
    print("x: \(point.x), y: \(point.y) => \(value)")
}



/*
run:

x: 3, y: 6 => B
x: 2, y: 7 => A
x: 0, y: 0 => C

*/

 



answered Aug 10 by avibootz
...