调用 API 数据并在表视图单元格中显示 Fateh。

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

have call api data and Fateh in table view cell

问题

我已运行此代码,但没有在控制台打印任何数据,也没有从我的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()
}

请注意,我只翻译了代码部分,没有包括注释。如果您需要更多帮助或有其他问题,请告诉我。

英文:

i have run this code but not print any data in console and not get data in my api so no data display in may simulators

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()
    }

}

答案1

得分: 1

以下是您要翻译的代码部分:

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数据名称相同的变量名,您可以像这样编写:

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"
    }
}

您还提供了关于您的表格视图屏幕的截图,但这不是可翻译的文本。如果您有其他需要翻译的文本,请随时提问。

英文:
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()
    }
}

above is my viewcontroller

> This is my model for api data

// 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
}

if you want more data then you can add variable and use name same as api data name

> if you don't want to use same as api data name so you can write like this

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"
    }
}

> And My tableView Screen Is Below
调用 API 数据并在表视图单元格中显示 Fateh。

I Hope You Need This

huangapple
  • 本文由 发表于 2023年2月24日 15:58:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553919.html
匿名

发表评论

匿名网友

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

确定