英文:
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 '[UpcomingEventData]' (aka 'Array<Array<UpcomingEventDatum>>') to expected argument type 'Binding<C>'
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<Swift.Array<Swift.Array<WW.UpcomingEventDatum>>>
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 = "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]
答案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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论