繁体   English   中英

SwiftUI 从 ZE6B391A8D2C4D45902A2Z3A8B6585703D 加载单个 object JSON

[英]SwiftUI Load single object JSON from URL

我正在尝试使用存储在网站上的单个 object JSON 文件。 我想在 ZStack 内的文本中显示 JSON 文件中的变量(点)之一。 但是定义@State 变量spotData 时存在问题。 SwiftUI 的错误:“调用中的参数 'from' 缺少参数”。 知道我做错了什么吗?

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)
    }
    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM