SwiftUI 从 URL 加载单个对象 JSON
Posted
技术标签:
【中文标题】SwiftUI 从 URL 加载单个对象 JSON【英文标题】:SwiftUI Load single object JSON from URL 【发布时间】: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()
提前致谢!
【问题讨论】:
这能回答你的问题吗? Missing argument for parameter 'from' in call - SwiftUI 【参考方案1】:你不能在不输入所有值的情况下初始化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)
【讨论】:
谢谢。它的工作。找了好几个小时了。 将所有内容都设为可选不是一个好的解决方案 是的,不好,但他想用一个空的构造函数,我给了他两种可能以上是关于SwiftUI 从 URL 加载单个对象 JSON的主要内容,如果未能解决你的问题,请参考以下文章