Created
April 18, 2015 23:54
-
-
Save derrickshowers/80fb450490ff03e6e274 to your computer and use it in GitHub Desktop.
Delegation Design Pattern Using Swift
This file contains 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