gpt4 book ai didi

ios - swift 将 JSON 数据显示到 socket

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:53 24 4
gpt4 key购买 nike

我对编码非常陌生,正在尝试一步一步学习。我搜索了一堆,发现了一些很棒的想法,但没有一个能回答我希望学习的问题。

我创建了一些代码,可以从单个 Web URL 中提取 JSON 天气数据。它只共享一个位置的天气数据,不请求该信息。

我能够打印 JSON 数据中的信息! (这是一个巨大的进步)。下一步是在我的应用程序和手机上显示来自 JSON 数据的信息。我在表格 View 中找到了很多关于这样做的帖子,但无法让它为我的应用程序工作,主要是因为其中许多要求您搜索位置以提取数据(这不适用于我的情况).

我想从 JSON 数据中提取一条信息并将其显示在我的屏幕上。

我在 Storyboard中创建了一个标签并将其连接到我的 View Controller 。

我还有调用 JSON 数据的 weather.swift。

我遇到的问题是我收到错误“'[Weather]' 类型的值没有成员'name'”。这是在我看来 controller.swift 的代码行“locationOutlet.text = observationsData.name”。

这是我的 View Controller 中的代码:

导入 UIKit

类 View Controller :UIViewController {

@IBOutlet weak var locationOutlet: UILabel!

var observationsData = [Weather]()


override func viewDidLoad() {
super.viewDidLoad()

/* Weather.observations(withLocation: ",") { (results:[Weather]) in
for result in results {
print("\(result)\n\n")
}
*/

}

// Do any additional setup after loading the view, typically from a nib.

func loadweatherData (location:String) {

Weather.observations(withLocation: ",") { (results:[Weather]?) in
if let weatherData = results {
self.observationsData = weatherData
}

//let weatherObject = observationsData[IndexPath.row]

}

func displayweatherData (location:String) {

locationOutlet.text = observationsData.name

}
//locationOutlet?.text = observationsData.name

}

}

这里是 weather.swift 中调用 JSON 数据的代码。

import Foundation

struct Weather {
let name:String
let air_temp:Double
let wind_spd_kt:Double
let swell_height:Double


enum SerializationError:Error {
case missing(String)
case invalid(String, Any)
}

init(json:[String:Any]) throws {
guard let name = json["name"] as? String else{throw SerializationError.missing("Name is missing")}

guard let air_temp = json["air_temp"] as? Double else{throw SerializationError.missing("Air temp is missing")}

guard let wind_spd_kt = json["wind_spd_kt"] as? Double else{throw SerializationError.missing("Wind speed is missing")}

guard let swell_height = json["swell_height"] as? Double else{throw SerializationError.missing("Swell hight is missing")}

self.name = name
self.air_temp = air_temp
self.wind_spd_kt = wind_spd_kt
self.swell_height = swell_height
}

static let basePath = "http://www.bom.gov.au/fwo/IDN60701/IDN60701.94937.json"

static func observations (withLocation location:String, completion: @escaping ([Weather]) -> ()) {

let url = basePath //+ location
let request = URLRequest(url: URL(string: url)!)

let task = URLSession.shared.dataTask(with: request) { (data:Data?, response:URLResponse?, error:Error?) in

var observationsArray:[Weather] = []

if let data = data {

do {
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any] {
if let currentObservations = json["observations"] as? [String:Any] {
if let currentData = currentObservations["data"] as? [[String:Any]] {
for dataPoint in currentData {
if let weatherObject = try? Weather(json: dataPoint) {
observationsArray.append(weatherObject)
}
}
}
}
}
}catch{
print(error.localizedDescription)
}

completion(observationsArray)

}
}

task.resume()
}

}

很明显,我正在学习并尝试做一些可能比我想象的更难的事情。任何关于如何让我的标签显示数据的帮助都会很棒。感谢您的宝贵时间!

最佳答案

然后您所要做的就是获取第一个索引的天气对象并将其属性显示在标签中。

Weather.observations(withLocation: ",") { (results) in
if let firstIndexWeatherData = results.first {
//As API calls are usually on background thread and Apple requires Main Thread to update the UI so set your weatherData name to label on main thread like this.
DispatchQueue.main.async {
self.locationOutlet.text = firstIndexWeatherData.name
}
}
}

关于ios - swift 将 JSON 数据显示到 socket ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54880329/

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