import UIKit

// JSON Structure

struct Jsonstruct:Decodable {
    let name:String
    let capital:String
    let alpha2Code:String
    let alpha3Code:String
    let region:String
    let subregion:String
    let flag : [URL]
}

class ViewController: UIViewController, UICollectionViewDataSource,UICollectionViewDelegate {

    @IBOutlet weak var collectionView: UICollectionView!
    // JSON Array
    var arrdata = [Jsonstruct]()

//    let foodName = ["Hawaii Resort", "Mountain Expedition", "Scuba Diving","Hawaii Resort", "Mountain Expedition", "Scuba Diving"]
//    let foodImage = [UIImage(named: "1"),UIImage(named: "2"),UIImage(named: "3"),UIImage(named: "1"),UIImage(named: "2"),UIImage(named: "3")]
//    let foodDescription =  ["Beautiful resort off the coast of Hawaii", "Exhilarating mountainous expedition through Yosemite National Park", "Awesome Scuba Diving adventure in the Gulf of Mexico","Beautiful resort off the coast of Hawaii", "Exhilarating mountainous expedition through Yosemite National Park", "Awesome Scuba Diving adventure in the Gulf of Mexico"]
//

    override func viewDidLoad() {
        super.viewDidLoad()

        //JSON CALL Function
        getdata()
    }

    func getdata(){
        let url = URL(string: "https://restcountries.eu/rest/v2/all")
        URLSession.shared.dataTask(with: url!) { (data, response, error) in
            do{if error == nil{
                self.arrdata = try JSONDecoder().decode([Jsonstruct].self, from: data!)

                for mainarr in self.arrdata{


                    //print(mainarr.name,":",mainarr.capital,":",mainarr.alpha3Code)
                    DispatchQueue.main.async {

                        self.collectionView.reloadData()
                    }

                }
                }

            }catch{
                print("Error in get json data")
            }

            }.resume()


    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //return foodName.count
        return self.arrdata.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell

        cell.CountryName.text = "Name : \(arrdata[indexPath.row].name)"
        cell.CountryCapital.text = "Capital : \(arrdata[indexPath.row].capital)"



        cell.contentView.layer.cornerRadius = 4.0
        cell.contentView.layer.borderWidth = 1.0
        cell.contentView.layer.borderColor = UIColor.clear.cgColor
        cell.contentView.layer.masksToBounds = false
        cell.layer.shadowColor = UIColor.gray.cgColor
        cell.layer.shadowOffset = CGSize(width: 0, height: 1.0)
        cell.layer.shadowRadius = 4.0
        cell.layer.shadowOpacity = 1.0
        cell.layer.masksToBounds = false
        cell.layer.shadowPath = UIBezierPath(roundedRect: cell.bounds,  cornerRadius: cell.contentView.layer.cornerRadius).cgPath



        return cell

    }
    }
  • 2
    You should ask a question and explain what you want reviewed. This appears to all be code. What do you want out of this post? If it works and you want feedback, then this is probably the right spot but explain what you want feedback on. If you are running in to error head over to stack overflow instead.You can make edits to this post by clicking the edit button below the post. – Albert Rothman Aug 27 at 21:49
  • I'd suggest reading codereview.stackexchange.com/help/how-to-ask also. – chicks Aug 28 at 2:38

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.