【问题标题】:How to pass the data to the tableview after getting URL on the terminal? Swift 5在终端上获取 URL 后如何将数据传递给 tableview?斯威夫特 5
【发布时间】:2019-12-29 03:43:42
【问题描述】:
import UIKit
import Alamofire
typealias JSON = [String: Any]

class NetworkingService {
    static let shared = NetworkingService()
    private init() {}

    func getPeople(completion: () -> Void) {
        AF.request(API.SpaceX.upcomingLaunches.url()).responseJSON{ (response) in

            switch response.result {
            case let .success(value):
                print(value)
            case let .failure(error):
                print(error)
            }
        }
    }
}

我从终端中的 api https://api.spacexdata.com/v3/launches/upcoming 获得了 JSON 数据。现在我想使用 swiftyJSON 或 Alamofire 解码数据并将其显示在我的 tableview 单元格上,但我无法做到。我想使用可编码的 alamofire,并且我已经在下面给出的另一个文件中编写了这些代码

    import Foundation

struct Mission: Codable {

    let flight_number: Int
    let mission_name: String
    let launch_date_utc: String
    let launch_date_local: String

    struct Rocket: Codable {
        let rocket_id: String
        let rocket_name: String
        let rocket_type: String
        let first_stage: FirstStage
        let second_stage: SecondStage
    }

    struct FirstStage: Codable {
        let cores: [Core]
    }

    struct Core: Codable {
        let core_serial: String?
        let flight: Int?
        let reused: Bool?
    }

    struct SecondStage: Codable {
        let payloads: [Payload]
    }

    struct Payload: Codable {
        let payload_id: String
        let reused: Bool?
        let customers: [String]
        let payload_type: String?
    }

    struct LaunchSite: Codable {
        let site_id: String
        let site_name: String
        let site_name_long: String
    }

    struct Links: Codable {
        let mission_patch: String?
        let presskit: String?
        let article_link: String?
        let video_link: String?
        let reddit_campaign: String?
    }

    let rocket: Rocket
    let launch_site: LaunchSite
    let links: Links
}

以上是我的可编码数据。我想在我的Tableview cell 中提供所有这些细节。你能帮我用 swiftyJSON 或 alamofire 重写代码吗? :)

import Foundation
struct API {
    enum SpaceX {
        case allLaunches
        case pastLaunches
        case upcomingLaunches
        case latestLaunch
        case nextLAunch

        func url() -> String {
            switch self {
            case .allLaunches:
                return "https://api.spacexdata.com/v3/launches"

            case .pastLaunches:
                return "https://api.spacexdata.com/v3/launches/past"

            case .upcomingLaunches:
                return "https://api.spacexdata.com/v3/launches/upcoming"


            case .latestLaunch:
                return "https://api.spacexdata.com/v3/launches/latest"

            case .nextLAunch:
                return "https://api.spacexdata.com/v3/launches/next"
            }
        }
    }
}

以上是我做的api func。

【问题讨论】:

  • 如果你使用Codable 结构那么你不需要SwiftyJson。您可以对来自 api 请求的数据使用 json 解码器。
  • @Paulw11 感谢您提供的信息,但如何编写 json 解码器?我看过 Alamofire 文档,但自从我学到了新知识后,我面临着一些困惑。你能帮我写代码吗?

标签: ios json swift xcode swift5


【解决方案1】:

在这里,首先您需要使用 JSONDecoder 将数据转换为自定义对象。为此,您需要像您已经使用的那样使用 Codable。这里也可以使用responseData代替responseJSON,比成功案例直接获取数据。

func getPeople(completion: () -> Void) {
    AF.request(API.SpaceX.upcomingLaunches.url()).responseJSON{ (response) in
        switch response.result {
        case let .success(value):
            print(value)
            if let data = response.data {
                // Convert to Object
                do {
                    let responseDecoded = try JSONDecoder().decode([Mission].self, from: data)
                    print(responseDecoded.count)
                } catch let error {
                    // Parser error
                    print(error)
                }
            }
        case let .failure(error):
            print(error)
        }
    }
}

我希望这会有所帮助。

【讨论】:

  • 这里的响应很复杂,包含数组中的对象。 Codable 在这里使用比较复杂,你需要照顾所有情况。在这里,您可以使用自己的自定义 initializer 将 JSON(Dictionary) 解析为对象。
  • 现在下一步该做什么?因为我想将信息传递给 tableview 单元格。接下来我该怎么办?
  • 关于你所说的可编码,它很难使用。好的,但是下一步怎么办?
  • @Eliza 您必须使用func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 来显示单元格。在您的单元格类中创建类似setDetails(_ mission: Mission) 的方法。仍然有问题,然后使用单元类更新您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 2017-03-24
  • 1970-01-01
相关资源
最近更新 更多