【发布时间】:2020-12-22 18:46:21
【问题描述】:
我正在尝试使用存储在网站上的单个对象 JSON 文件。我想在 ZStack 内的 Text 中显示 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()
}
}
提前致谢!
【问题讨论】: