SwiftUI 从 URL 加载单个对象 JSON

问题描述

我正在尝试使用存储在网站上的单个对象 JSON 文件。我想在 ZStack 内的文本中显示来自 JSON 文件的变量之一(点)。然而,定义@State 变量spotData 存在问题。 SwiftUI 的错误:“调用中缺少参数‘来自’的参数”。 知道我做错了什么吗?

import SwiftUI
import CoreLocation

struct SpotStructure: Decodable {
    
    var spot: String
    var country_code: String
    var is_weather_station: Bool
    var month: [Int]
    var day: [Int]
    var weekday: [String]
    var hour: [Int]
    var current_wind: Int
    var current_wind_direction: Int
    var wind_speed: [Int]
    var wind_gusts: [Int]
    var wind_direction: [Int]
    var wave_height: [Double]
    var wave_period: [Int]
    var wave_direction: [Int]
    var weather_icon: [String]
    var current_temperature: Int
    var temperature: [Int]
    
    private var coordinates: Coordinates
    var locationCoordinate: CLLocationCoordinate2D {
        CLLocationCoordinate2D(
            latitude: coordinates.latitude,longitude: coordinates.longitude)
    }

    struct Coordinates: Hashable,Codable {
        var latitude: Double
        var longitude: Double
    }
}

struct ContentView: View {
    @State var spotData = SpotStructure()
    
    func loadspotdata()  {
        if let url = URL(string: "https://raw.githubusercontent.com/Bene2907/Bene2907.github.io/master/brouwersdam.json") {
            URLSession.shared.dataTask(with: url) { data,response,error in
                if let data = data {
                    do {
                        let res = try JSONDecoder().decode(SpotStructure.self,from: data)
                        spotData = res
                    } catch let error {
                        print(error)
                    }
                }
                
            }
        }
    }

    var body: some View {
        ZStack{

            Text(spotData.spot)

        }.onAppear(perform: loadspotdata)
    }
}

struct ContentView_Previews: PreviewProvider {
    
    static var previews: some View {
        ContentView()
    }
}

提前致谢!

解决方法

你不能在不放入所有值的情况下初始化struct(因为所有变量都不是可选的!

@State var spotData = SpotStructure(spot: <#T##String#>,country_code: <#T##String#>,is_weather_station: <#T##Bool#>,month: <#T##[Int]#>,day: <#T##[Int]#>,weekday: <#T##[String]#>,hour: <#T##[Int]#>,current_wind: <#T##Int#>,current_wind_direction: <#T##Int#>,wind_speed: <#T##[Int]#>,wind_gusts: <#T##[Int]#>,wind_direction: <#T##[Int]#>,wave_height: <#T##[Double]#>,wave_period: <#T##[Int]#>,wave_direction: <#T##[Int]#>,weather_icon: <#T##[String]#>,current_temperature: <#T##Int#>,temperature: <#T##[Int]#>,coordinates: <#T##Coordinates#>) 

或者更改您的 struct 并使所有变量可选

struct SpotStructure: Codable {
    var spot: String? = nil
    var country_code: String? = nil
    var is_weather_station: Bool? = nil
    var month: [Int]? = nil
    var day: [Int]? = nil
    var weekday: [String]? = nil
    var hour: [Int]? = nil
    var current_wind: Int? = nil
    var current_wind_direction: Int? = nil
    var wind_speed: [Int]? = nil
    var wind_gusts: [Int]? = nil
    var wind_direction: [Int]? = nil
    var wave_height: [Double]? = nil
    var wave_period: [Int]? = nil
    var wave_direction: [Int]? = nil
    var weather_icon: [String]? = nil
    var current_temperature: Int? = nil
    var temperature: [Int]? = nil
    
    var coordinates: Coordinates? = nil
    var locationCoordinate: CLLocationCoordinate2D? {
        CLLocationCoordinate2D(
            latitude: coordinates!.latitude,longitude: coordinates!.longitude)
    }
    
}