Created
February 8, 2016 15:12
-
-
Save MathieuWhite/0708f26c459d87e5b1c4 to your computer and use it in GitHub Desktop.
100% Optional Delegate Functions in 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
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