Home > Enterprise >  Swift .map not providing expected data type
Swift .map not providing expected data type

Time:01-22

I am trying to covert two different arrays of double into an array of MKMapPoint. I have an x array and y array. Both are nested inside of another array that holds other data types such as Strings and bools.

Here is the model:

struct HostModel: Identifiable, Codable {
    var id: String
    var fenceX: [Double]
    var fenceY: [Double]
    var eventName: String
    var eventDescription: String
    var isEventPrivate: Bool
    
    private enum CodingKeys: String, CodingKey {
        case id
        case fenceX
        case fenceY
        case eventName
        case eventDescription
        case isEventPrivate
    }
}
for data in fenceData {
     let x = data.fenceX.map({ $0.self })
     let y = data.fenceY.map({ $0.self })
     print("\(x)")
     allMapPoints.append(MKMapPoint(x: x, y: y))
}

here is the data type $0.self shows

I would think that this code would work since $0.self says it is a Double(see image above), however when I try to put it in the mkmappoint it gives me an error saying "Cannot convert value of type '[Double]' to expected argument type 'Double'"

And the print statement shows this array:

[46476022.57489817, 46475952.06747695, 46475920.9612687, 46475989.39492689, 46476067.16044753, 46476150.11035207, 46476199.88028529, 46476096.19291651]

Why is .map not providing the data type it says it does?

CodePudding user response:

That should be pretty simple. What you actually need is to zip your x and y coordinates and map the MKMapPoint initializer:

let allMapPoints = zip(data.fenceX, data.fenceY).map(MKMapPoint.init)
  •  Tags:  
  • Related