swift 在表格视图单元格中调用API数据和Fateh

sczxawaw  于 2023-03-07  发布在  Swift
关注(0)|答案(1)|浏览(131)

我已经运行了这段代码,但没有在控制台打印任何数据,也没有在我的API中获取数据,因此在五月模拟器中没有显示数据

func downloadJSON(completed: @escaping () -> ()){
        let url = URL(string: "https://gydo.me/api/api/User/index")

        URLSession.shared.dataTask(with: url!) {data, response, error in
           guard let data = data, error == nil else {return}
          //print(data)

            if error == nil {
                do{
                    if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]{
                        print(json)
                   self.detail = try JSONDecoder().decode([apiData].self, from: data)
                       
                    DispatchQueue.main.async {
                        completed()
                    }
                    }
                }catch{
                    print("JSON Error")
                }
            }
        }.resume()
    }

}

func downloadJSON(completed: @escaping () -> ()){
        let url = URL(string: "https://gydo.me/api/api/User/index")

        URLSession.shared.dataTask(with: url!) {data, response, error in
           guard let data = data, error == nil else {return}
          //print(data)

            if error == nil {
                do{
                    if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]{
                        print(json)
                   self.detail = try JSONDecoder().decode([apiData].self, from: data)
                       
                    DispatchQueue.main.async {
                        completed()
                    }
                    }
                }catch{
                    print("JSON Error")
                }
            }
        }.resume()
    }

}
hc8w905p

hc8w905p1#

class ViewController: UIViewController {

    // MARK: - Iboutlet
    @IBOutlet weak var apiDataTableView: UITableView!

    // MARK: - Varaible
    var apiData = [DataModel]()

    // MARK: - Life CYcle Method
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        intlizer()
    }

    // MARK: - Intlization
    func intlizer() {
        downloadJSON {
            self.apiDataTableView.reloadData()
        }
    }

    // MARK: - Make A Api Call
    func downloadJSON(completed: @escaping () -> ()){
        let url = URL(string: "https://gydo.me/api/api/User/index")
        URLSession.shared.dataTask(with: url!) {data, response, error in
              guard let data = data, error == nil else {return}
             //print(data)

               if error == nil {
                   do{
                      let decoder = JSONDecoder()
                      let decodedData = try 
decoder.decode(ApiDataModel.self, from: data)
                      self.apiData = decodedData.message
                      print(self.apiData)
                      
                      DispatchQueue.main.async {
                           completed()
                      }
                   } catch {
                       print("JSON Error")
                   }
               }
        }.resume()
    }
}

上面是我的视图控制器
这是我的API数据模型

// MARK: - Model
struct ApiDataModel: Decodable {
    var success: Bool
    var message: [DataModel]
}

struct DataModel: Decodable {
    var id: String
    var ip_address: String
    var access_token: String
}

如果你想要更多数据,那么你可以添加变量并使用与API数据名相同的名称
如果您不想使用与API数据名称相同名称,则可以这样编写

struct DataModel: Decodable {
    var id: String
    var ip_address: String
    var access_token: String

    enum Codingkeys: String, CodingKey {
        case id
        case ipAddress = "ip_address"
        case accessToken = "access_token"
    }
}

我的tableView屏幕低于

希望你需要这个

相关问题