Last active
June 3, 2016 21:26
-
-
Save uberbruns/8b56077e713a5a6e72aeb478de68d3d8 to your computer and use it in GitHub Desktop.
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 | |
// Message Type | |
struct Message<Target> { | |
let action: (Target) -> () | |
init(_ action: (Target) -> ()) { | |
self.action = action | |
} | |
func deliverAlong(chain: [Any]) { | |
for object in chain { | |
if let receiver = object as? Target { | |
action(receiver) | |
} | |
} | |
} | |
} | |
// Target Protocol | |
protocol DoesSomething { | |
func doSomething() | |
} | |
// Receiver | |
class ReceivingObject : DoesSomething { | |
func doSomething() { | |
print("Message Received!") | |
} | |
} | |
// Chain with receiver | |
let chain: [Any] = [ReceivingObject()] | |
// Create a message | |
let message = Message<DoesSomething>({ $0.doSomething() }) | |
// Send message | |
message.deliverAlong(chain) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment