-1

In a separate data file called data.swift I have this code

struct Response: Decodable {
    var data: Data
}
struct Data: Decodable {
    var search: search
}

struct search: Decodable {
    var __Typename: String
    var query: String
    var searchResults: searchResults
}

...and so on and so forth. I then decode the data from a Rapid-Api like so

let session = URLSession.shared
        let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
            if (error != nil) {
                print(error!)
            } else {
                let products = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)
                if let water = products {
                    print("JSON: \n" + String(describing: water) + "\n")
                }
                
            }
        })

How do I display the data elements in ProductList.swift it's a (view) file. The API works as expected and displays the JSON in the terminal. I am using XCODE 12.4 as I am not permitted to upgrade any further.

anon867
  • 22
  • 4
  • There are many different ways to display data. Perhaps you want a `UITableView`? A `UIStackView`? `UICollectionView`? Just `UILabel`? Without more detail, this question is too vague. – jnpdx Sep 02 '22 at 05:06
  • @jnpdx it's my first app. How do I display the data – anon867 Sep 02 '22 at 05:10
  • I suggest you check out some UIKit tutorials online. There are hundreds (thousands?) out there. Hacking with Swift, for example, is a great resource. – jnpdx Sep 02 '22 at 05:12
  • 1
    `struct Data: Decodable`: I strongly suggest you to rename that because it exists already in `Foundation`, and you'll have conflicts. If you use `(De)Codable`, use `JSONDecoder`, not `JSONSerialization`. – Larme Sep 02 '22 at 07:22

1 Answers1

0

So, actually you want to receive the data in Model and show in view, either a label or image.

In your productSwift.List:

var response: Response?

Right now, you have to decode the data in the Model:

  static func postApiCall<T: Decodable>(completion: @escaping (Result<T,Error>) -> Void) {

let url = URL(string: "Enter Your URL here")
let request = URLRequest(url: url!)

let dataTask = URLSession.shared.dataTask(with: request) { data, response , error  in
    guard let data = data else {
        if error == nil {
            completion(.failure(error as! Error))
        }
        return
    }
    do {
        let decoder = JSONDecoder()
        let json = try decoder.decode(T.self, from: data)
        completion(.success(json))
    } catch let error {
        print(error.localizedDescription)
    }
}
dataTask.resume()

}

}

Now in your ProductList.swift:

  ServiceManage.postApiCall { (result : Result<Response,Error>) in
        switch result {
        case .success(let result):
            print("result is \(result)")
            self.response = response.data
            self.yourLabel.text = response.data.search.query
        case .failure(let failure):
            print(failure)
    }
    }

and as Larme said, change your Struct "Data" name to something else.