Created
July 21, 2014 15:19
-
-
Save pocketkk/d4ced019a7e5086bf677 to your computer and use it in GitHub Desktop.
Swift - Delegate Protocol Example
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 TodoDelegate { | |
func sendCompleted(todo: Todo) | |
} | |
class Notifications : TodoDelegate { | |
var message: String? | |
var sent: Bool = false | |
init() { | |
} | |
func sendCompleted(todo: Todo) { | |
println("Completed: \(todo.task)") | |
} | |
} | |
class Todo { | |
var task: String | |
var completed: Bool = false | |
var delegate : TodoDelegate? | |
init(task_name: String) { | |
task = task_name | |
} | |
func complete() { | |
if completed == false { | |
completed = true | |
delegate?.sendCompleted(self) | |
} | |
} | |
} | |
let del = Notifications() | |
let td = Todo(task_name: "Get Cheese") | |
td.delegate = del | |
td.complete() | |
let tr = Todo(task_name: "Dont' forget the honey") | |
tr.delegate = del | |
tr.complete() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment