【发布时间】:2021-01-24 19:29:27
【问题描述】:
我有一个具有“纬度”和“经度”属性的核心数据实体,我想将其数据读入 MapKit 的 MapAnnotation 的 []。我了解如何使用 ForEach 遍历实体以创建视图,如下面的示例所示,但我不明白如何将数据读入 []。
ForEach(stores.reversed()) { store in
HStack {
Text("\(store.name ?? "")")
Spacer()
Text("\(store.latitude, specifier: "%.3f"),")
Text("\(store.longitude, specifier: "%.3f")")
}
}
“locations”是一个包含 MapAnnotation 坐标的变量。 “stores”是一个包含获取的实体数据的变量。 “位置”是 CLLocationCoordinate2D 格式的可识别对象。
以下是我尝试过的,但显然它是错误的。如何正确遍历“商店”?
@State var locations: [Location] = [
for store in stores {
Location(coordinate: .init(latitude: store.latitude, longitude: store.longitude))
}
]
【问题讨论】: