SwiftUI:遍历JSON响应

huangapple go评论59阅读模式
英文:

SwiftUI: Iterate over JSON Response

问题

我是新手使用SwiftUI,正在尝试理解如何迭代遍历来自API响应的数组数组。

我已经解码了响应,数据在debugPrint(upcomingEvents)中看起来很好。

尝试解包可选项,并按照一些指南进行操作,但尚未成功。

视图中的错误:

无法将类型为'[UpcomingEventData]'(也称为'Array<Array<UpcomingEventDatum>>')的值转换为预期的参数类型'Binding<C>'

错误发生在VStack中的ForEach尝试:

@State private var upcomingEvents: [UpcomingEventData]?
....
VStack {
    if let events = upcomingEvents {
         ForEach(events) { event in
            Text(event)
        }
    }
}
.task {
    do {
        upcomingEvents = try await asyncUE.fetchWP()
...

upcomingEvents确实包含数据,debugPrint(type(of: upcomingEvents))
Swift.Optional<Swift.Array<Swift.Array<WW.UpcomingEventDatum>>>

模型:

// MARK: - UpcomingEventDatum
struct UpcomingEventDatum: Codable, Identifiable {
    let name: String
    let id: Int
    let txt, closeDate, headliners: String
    let logo: String
    let athleteImgOne: String
    let athleteAltOne: String
    let athleteImgTwo: String
    let athleteAltTwo: String
    let products: [Product]
    let url: String

    enum CodingKeys: String, CodingKey {
        case name, id, txt
        case closeDate = "close_date"
        case headliners, logo
        case athleteImgOne = "athlete_img_one"
        case athleteAltOne = "athlete_alt_one"
        case athleteImgTwo = "athlete_img_two"
        case athleteAltTwo = "athlete_alt_two"
        case products, url
    }
}

// MARK: - Product
struct Product: Codable, Identifiable {
    let btnTxt, url, id: String

    enum CodingKeys: String, CodingKey {
        case btnTxt = "btn_txt"
        case url, id
    }
}

typealias UpcomingEventData = [UpcomingEventDatum]
英文:

I'm new to SwiftUI and I'm trying to understand how to iterate over an array of arrays from an API response.

I have decoded the response and the data looks good in debugPrint(upcomingEvents)

Tried unwrapping optional, and following a few guides with no luck yet.

Error in View:

Cannot convert value of type &#39;[UpcomingEventData]&#39; (aka &#39;Array&lt;Array&lt;UpcomingEventDatum&gt;&gt;&#39;) to expected argument type &#39;Binding&lt;C&gt;&#39;

The error happens here in VStack on ForEach attempt:

  @State private var upcomingEvents: [UpcomingEventData]?
      ....
            VStack {
                if let events = upcomingEvents {
                     ForEach(events) { event in
                        Text(event)
                    }
                }
            }
            .task {
                do {
                    upcomingEvents = try await asyncUE.fetchWP()
       ...

upcomingEvents does have data and debugPrint(type(of: upcomingEvents))
Swift.Optional&lt;Swift.Array&lt;Swift.Array&lt;WW.UpcomingEventDatum&gt;&gt;&gt;

Model:

// MARK: - UpcomingEventDatum
struct UpcomingEventDatum: Codable, Identifiable {
    let name: String
    let id: Int
    let txt, closeDate, headliners: String
    let logo: String
    let athleteImgOne: String
    let athleteAltOne: String
    let athleteImgTwo: String
    let athleteAltTwo: String
    let products: [Product]
    let url: String

    enum CodingKeys: String, CodingKey {
        case name, id, txt
        case closeDate = &quot;close_date&quot;
        case headliners, logo
        case athleteImgOne = &quot;athlete_img_one&quot;
        case athleteAltOne = &quot;athlete_alt_one&quot;
        case athleteImgTwo = &quot;athlete_img_two&quot;
        case athleteAltTwo = &quot;athlete_alt_two&quot;
        case products, url
    }
}

// MARK: - Product
struct Product: Codable, Identifiable {
    let btnTxt, url, id: String

    enum CodingKeys: String, CodingKey {
        case btnTxt = &quot;btn_txt&quot;
        case url, id
    }
}

typealias UpcomingEventData = [UpcomingEventDatum]

答案1

得分: 0

目前您的 upcomingEvents 是一个 UpcomingEventDatum 数组的数组。

要消除错误,请使用:

@State private var upcomingEvents: UpcomingEventData?

Text(event.name)
英文:

Currently your upcomingEvents is an array of array of UpcomingEventDatum.

To remove the error use:

  @State private var upcomingEvents: UpcomingEventData?

and

  Text(event.name)

huangapple
  • 本文由 发表于 2023年6月29日 07:08:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定