Last active
December 24, 2015 13:14
-
-
Save hoorace/7f1ddcfb827455b8a6f4 to your computer and use it in GitHub Desktop.
swift anonymous class 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
| protocol HelloListener { | |
| func hello(title: String) | |
| } | |
| var list :[HelloListener] = [HelloListener]() | |
| var parentsHello: HelloListener = { | |
| class AnonymousClass: HelloListener { | |
| func hello(title: String) { | |
| print("My parents has son name is \(title)") | |
| } | |
| } | |
| return AnonymousClass() | |
| }() | |
| var selfHello: HelloListener = { | |
| class AnonymousClass: HelloListener { | |
| func hello(title: String) { | |
| print("my name is \(title)") | |
| } | |
| } | |
| return AnonymousClass() | |
| }() | |
| list.append(parentsHello) | |
| list.append(selfHello) | |
| for listener in list{ | |
| listener.hello("longhao") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment