Skip to content

Instantly share code, notes, and snippets.

@MathieuWhite
Created February 8, 2016 15:12
Show Gist options
  • Save MathieuWhite/0708f26c459d87e5b1c4 to your computer and use it in GitHub Desktop.
Save MathieuWhite/0708f26c459d87e5b1c4 to your computer and use it in GitHub Desktop.
100% Optional Delegate Functions in Swift
protocol SomeDelegate: class
{
func function()
func optionalFunction()
}
extension SomeDelegate
{
func optionalFunction() { print("This will get called if the function isn't implemented") }
}
class SomeClass
{
weak var delegate: SomeDelegate?
func fireDelegates()
{
self.delegate?.function()
self.delegate?.optionalFunction()
}
}
class DelegateClass: SomeDelegate
{
init()
{
let some: SomeClass = SomeClass()
some.delegate = self
some.fireDelegates()
}
func function()
{
print("the required delegate function")
}
func optionalFunction()
{
print("the optional delegate function")
}
}
let delegateClass = DelegateClass()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment