0

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

}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • `if let json = try JSONSerialization ... as? [String: Any]` contradicts `JSONDecoder().decode([apiData].self`. Ie, you are expecting to decode with `JSONDecoder` an Array, but with JSONSerialization a Dictionary. Remove the `if let json` line, or at least add an `else { print("Response is not a JSON OR a [String: Any]"); print("Received content: \(String(data: data, encoding: .utf8)")}` – Larme Feb 24 '23 at 08:15
  • According to the URL, it seems to be a `Dictionary` at top level, so `print(json)` should be called. But is `error` not `nil`? Is `data` not nil? – Larme Feb 24 '23 at 08:20

1 Answers1

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

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 enter image description here

I Hope You Need This