Skip to content

Instantly share code, notes, and snippets.

@pocketkk
Created July 21, 2014 15:19
Show Gist options
  • Save pocketkk/d4ced019a7e5086bf677 to your computer and use it in GitHub Desktop.
Save pocketkk/d4ced019a7e5086bf677 to your computer and use it in GitHub Desktop.
Swift - Delegate Protocol Example
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