我正在尝试从 JSON 获取数据。到目前为止,我可以提取并打印它。
我现在的目标是能够在我的 ContentView 中使用它,以便可以在 Text View 或类似的东西中使用它。我尝试创建@State 变量,将其作为参数传递等等,但似乎没有任何效果。
我对 SwiftUI 还很陌生,所以非常感谢您的帮助!
struct GeoService: Codable {
var status: String
var results: [GeoResult]
}
struct GeoResult: Codable {
struct Geometry: Codable {
struct Location: Codable {
let lat: Float
let lng: Float
init() {
lat = 32
lng = 30
}
}
let location: Location
}
let formatted_address: String
let geometry: Geometry
}
struct ContentView: View {
// @State private var results: Any ?????????
var body: some View {
NavigationView {
Text("Test")
.navigationTitle("Quotes")
.task {
await handleData()
}
}
}
func handleData() async {
let geoResult="""
{
"results": [
{
"formatted_address": "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry": {
"location": {
"lat": 37.4224764,
"lng": -122.0842499
}
}
},
{
"formatted_address": "Test addresss",
"geometry": {
"location": {
"lat": 120.32132145,
"lng": -43.90235469
}
}
}
],
"status": "OK"
}
""".data(using: .utf8)!
let decoder = JSONDecoder()
print("executing handleData()")
do {
let obj = try decoder.decode(GeoService.self, from: geoResult)
for result in obj.results {
print("Address: \(result.formatted_address)")
print("Lat/long: (\(result.geometry.location.lat), \(result.geometry.location.lng))")
}
} catch {
print("Did not work :(")
}
}
}
回答1
我将 get 请求移到了它自己的类中,以便我们可以利用 ObservableObject
和 Publish
数据。发布数据允许订阅视图在发布的数据更改时更新数据。在这种情况下,您的 ContentView
正在通过 @ObservedObject var geoData: ResponseData
代码行订阅数据。
此外,我添加了如何访问视图中的两条相关数据,并将其显示在列表中以便于阅读。这应该让您了解如何访问/显示数据。
希望这为您提供了足够的信息来调整答案以使其按您希望的方式工作。
import SwiftUI
struct GeoService: Codable {
var status: String?
var results: [GeoResult]?
}
struct GeoResult: Codable {
struct Geometry: Codable {
struct Location: Codable {
let lat: Float
let lng: Float
init() {
lat = 32
lng = 30
}
}
let location: Location
}
let formatted_address: String
let geometry: Geometry
}
struct ContentView: View {
@ObservedObject var geoData: ResponseData
var body: some View {
NavigationView {
if #available(iOS 15.0, *) {
List {
Text(geoData.geoResultsData?.results?[0].formatted_address ?? "Loading")
Text(String(geoData.geoResultsData?.results?[0].geometry.location.lat ?? 0))
}
.navigationTitle("Quotes")
.task {
await geoData.handleData()
print(geoData.geoResultsData, "yessssss")
}
} else {
Text("failure")
}
}
}
}
class ResponseData: ObservableObject {
@Published var geoResultsData: GeoService?
func handleData() async {
let geoResult="""
{
"results": [
{
"formatted_address": "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry": {
"location": {
"lat": 37.4224764,
"lng": -122.0842499
}
}
},
{
"formatted_address": "Test addresss",
"geometry": {
"location": {
"lat": 120.32132145,
"lng": -43.90235469
}
}
}
],
"status": "OK"
}
""".data(using: .utf8)!
let decoder = JSONDecoder()
print("executing handleData()")
do {
let obj = try decoder.decode(GeoService.self, from: geoResult)
geoResultsData = obj
} catch {
print("Did not work :(")
}
}
}
编辑:
您将需要在您的应用程序中初始化一些数据。如果需要,您可以暂时将其初始化为空。这可以通过执行以下操作来完成:ContentView(geoData: ResponseData.init())