Skip to content

Instantly share code, notes, and snippets.

@takaheraw
Created November 15, 2015 01:43
Show Gist options
  • Save takaheraw/585bcf4c1196d570a6be to your computer and use it in GitHub Desktop.
Save takaheraw/585bcf4c1196d570a6be to your computer and use it in GitHub Desktop.
struct Message {
let from: String
let to: String
let subject: String
}
class LocalTransmitter {
func sendMessage(message: Message) {
print("Message to \(message.to) sent locally")
}
}
class RemoteTransMitter {
func sendMessage(message: Message) {
print("Message to \(message.to) send remotely")
}
}
let messages = [
Message(from: "[email protected]", to: "[email protected]", subject: "Free for lunch"),
Message(from: "[email protected]", to: "[email protected]", subject: "New Contracts"),
Message(from: "[email protected]", to: "[email protected]", subject: "All-Hands Meeting")
]
let localT = LocalTransmitter()
let remoteT = RemoteTransMitter()
for message in messages {
if let index = message.from.rangeOfString("@") {
if (message.to.hasSuffix(message.from[Range<String.Index>(start: index.startIndex, end: message.from.endIndex)])) {
localT.sendMessage(message)
} else {
remoteT.sendMessage(message)
}
} else {
print("Error: cannot send message to \(message.from)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment