Forked from derrickshowers/gist:80fb450490ff03e6e274
Last active
June 3, 2016 04:58
-
-
Save pingzh/2694bd711ad24a0d51d0 to your computer and use it in GitHub Desktop.
Delegation Design Pattern Using Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
protocol YelpRequestDelegate { | |
func getYelpData() -> AnyObject | |
func processYelpData(data: NSData) -> NSData | |
} | |
class YelpAPI { | |
var delegate: YelpRequestDelegate? | |
func getData() { | |
println("data being retrieved...") | |
let data: AnyObject? = delegate?.getYelpData() | |
} | |
func processYelpData(data: NSData) { | |
println("data being processed...") | |
let data = delegate?.processYelpData(data) | |
} | |
} | |
class Controller: YelpRequestDelegate { | |
init() { | |
var yelpAPI = YelpAPI() | |
yelpAPI.delegate = self | |
yelpAPI.getData() | |
} | |
func getYelpData() -> AnyObject { | |
println("getYelpData called") | |
return NSData() | |
} | |
func processYelpData(data: NSData) -> NSData { | |
println("processYelpData called") | |
return NSData() | |
} | |
} | |
var controller = Controller() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment