gpt4 book ai didi

swift - 循环遍历 MapKit 的 JSON 数据不起作用

转载 作者:行者123 更新时间:2023-11-30 10:28:03 25 4
gpt4 key购买 nike

目前我正在编写我的第一个大型 iOS 应用程序,我期待在不久的将来发布。我自己无法解决的挑战如下:

鉴于我的 Web 服务 WebServicePins.swift,我正在从我自己的 API 获取数据。它看起来像这样:

[
{
"id": 1,
"title": "Berlin",
"body": "Capital of Germany",
"thumb": "*Link to my AWS bucket*",
"lng_coord": "13.404954",
"lat_coord": "52.520008",
"date": "2019-11-10T11:36:31.409361Z",
"owner": 1,
"type": "other"
},
{
"id": 4,
"title": "Munich",
"body": "Home of the Oktoberfest",
"thumb": "*Link to my AWS bucket*",
"lng_coord": "11.576124",
"lat_coord": "48.137154",
"date": "2019-11-10T11:36:31.409361Z",
"owner": 1,
"type": "other"
}
]

等等。 Web 服务工作正常,我使用 PinListViewModel.swift 中的数据:

import Foundation

class PinListViewModel: ObservableObject {
@Published var pins = [PinViewModel]()

init() {

WebservicePins().getPins { pins in

if let pins = pins {
self.pins = pins.map(PinViewModel.init)
}
}
}
}

struct PinViewModel: Identifiable {

var pin: Pin

init(pin: Pin) {
self.pin = pin
}

var id: Int {
return self.pin.id
}

var title: String {
return self.pin.title
}

var body: String {
return self.pin.body
}

var thumb: String {
return self.pin.thumb
}

var lng_coord: String {
return self.pin.lng_coord
}

var lat_coord: String {
return self.pin.lat_coord
}

var date: String {
return self.pin.date
}

var owner: Int {
return self.pin.owner
}

var type: String {
return self.pin.type
}

}

使用 ObservableObject 在 SwiftUI View 中显示列表

struct ArticlesTabView: View {

@ObservedObject private var pinListVM = PinListViewModel()

var body: some View {
VStack {
List(self.pinListVM.articles, id: \.id) { pin in
Text(pin.title)
}
}
}
}

按预期工作,但是我无法循环数组,例如

func makeUIView(context: Context) -> MKMapView {

let pinListVM = PinListViewModel()

let mapView = MKMapView()
mapView.delegate = context.coordinator

// Test Annotations
let testPins = [
Pin(id: 1, title: "Berlin", body: "Capital of Germany",
thumb: "", lng_coord: "13.404954", lat_coord: "52.520008", date: "",
owner: 1, type: "action"),
Pin(id: 2, title: "München", body: "Home of the Oktoberfest",
thumb: "", lng_coord: "11.576124", lat_coord: "48.137154", date: "",
owner: 1, type: "other")
]

for pin in pinListVM.pins {
let annotation = MKPointAnnotation()
annotation.title = pin.title
annotation.subtitle = pin.body
annotation.coordinate = CLLocationCoordinate2D(latitude: Double(pin.lat_coord)!, longitude: Double(pin.lng_coord)!)
mapView.addAnnotation(annotation)
}

for testPin in testPins {
let annotation = MKPointAnnotation()
annotation.title = testPin.title
annotation.subtitle = testPin.body
annotation.coordinate = CLLocationCoordinate2D(latitude: Double(testPin.lat_coord)!, longitude: Double(testPin.lng_coord)!)
// annotation.pinTintColor = UIColor.green
mapView.addAnnotation(annotation)
}

return mapView
}

testPins 正在显示,而 pin 并未显示在最终 map 中。你能帮我找出我的错误吗?先感谢您!PS:我是 stackoverflow.com 的新手,请随时更改我的问题/告诉我如何使其更具描述性或更容易理解!

最佳答案

看起来您正在 map View 中创建一个新的 PinListViewModel。我认为你应该将其设为@ObservedObject。

关于swift - 循环遍历 MapKit 的 JSON 数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59720823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com