arkilis
4/26/2017 - 11:56 PM

delegation swift WeatherService viewcontroller

delegation swift WeatherService viewcontroller

// There are two ways of doing this:


// Way 1: use swift's multi-inheritance 

class ViewController: UIViewController, WeatherServiceDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let weather = WeatherService()
        weather.delegate = self
        weather.fetchDataFromURL(url: "Some data")
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func didCompleteRequest(result: String){
        print(result)
    }
  
  
// Way 2: use extension
  
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let weather = WeatherService()
        weather.delegate = self
        weather.fetchDataFromURL(url: "Some data")
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}


extension ViewController: WeatherServiceDelegate{
    func didCompleteRequest(result: String){
        print(result)
    }
}