Friday, 6 April 2018

MVC Structure Demo

Step - 1 : Create class with .swift file. Like,


import UIKit

class Country: NSObject {

    var countryName:String?
    var capital:String?
    var flag:String?
    var lat:Int?
    var long:[Any]?
   
    init?(countryDetail:[String:Any]) {
        let dict = countryDetail
        
        self.countryName = (dict["name"] as! String)
        self.capital = (dict["capital"] as! String)
        self.flag = dict["flag"] as! String
        super.init()
    }
   
}



Step - 2 : In ViewController put following code. Like,


import UIKit
import Alamofire
import SDWebImage

class ViewController: UIViewController,UITableViewDataSource {
    
    
    var array = [[String:Any]]()
    var temp = [[String:Any]]()
    
    var country:[Country] = [Country]()
    //let url:URL = URL.init(string: "https://restcountries.eu/rest/v2/all")!
    @IBOutlet weak var tblCountry: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.getApi()
        
       /* Alamofire.request(URL.init(string: "https://restcountries.eu/rest/v2/all")!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
            
            switch response.result
            {
            case .success:
                var json = response.result.value
                self.array = json as! [[String:Any]]
                print(self.array.count)
                print(self.array)
                //let record = (self.array.first as! [String:Any])["name"] as! String
                
               // print(record)
                
                for i in self.array
                {
                    
                    self.temp.append(i)
                    //let strconutryname = (i as! [String:Any])
                    
                   // print(strconutryname["capital"] as! String)
                }
                break
                
            case .failure(let error):
                print(error)
            }
        }*/
        
        tblCountry.dataSource = self
        
        
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func viewDidAppear(_ animated: Bool) {
        
        //self.tblCountry.reloadData()
    }
    func getApi(){
        
       

         Alamofire.request(URL.init(string: "https://restcountries.eu/rest/v2/all")!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in

            switch response.result
            {
            case .success:
                
                let json = response.result.value
                print(json as Any)
                self.array = json as! [[String : Any]]
                //print(self.array.count)
                //let countryName = (self.array.first as! [String:Any])["name"] as! String
                
                //print("Country Name:\(countryName)")
                
                for countryDetail in self.array
                {
                    if let information = Country.init(countryDetail: countryDetail)
                    {
                        self.country.append(information)
                        
                    }
                }
                self.tblCountry.reloadData()
                //self.tblCountry.reloadData()
                break

            case .failure(var error):
                print(error)
            }
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //print(array.count)
        //return country.count
        return country.count
        
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:CountryCell = tableView.dequeueReusableCell(withIdentifier: "CountryCell") as! CountryCell
        
        //cell.lblName.text = self.array[indexPath.row]["name"] as? String
        cell.lblName.text = country[indexPath.row].countryName
        if let url:URL = URL.init(string: country[indexPath.row].flag!){
            cell.imgFlag.sd_setImage(with: url, placeholderImage:  imageLiteral(resourceName: "placeholder"), options: SDWebImageOptions.continueInBackground, completed: { (image, error, catchType, url) in
                if error == nil {
                    cell.imgFlag.image = image
                }
            })
            
        }
        return cell
    }


Thank you.

No comments:

Post a Comment