Created
June 8, 2015 12:05
-
-
Save mgrebenets/ae93a434f3b15026c150 to your computer and use it in GitHub Desktop.
Swift Dictionary Literal Convertible
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
import Foundation | |
/// Timeout Options | |
struct Options { | |
let timeout: Double = 1 // timeout in seconds | |
let message = "Timeout failed." // message to display on timeout | |
} | |
/// Wait for something with timeout options | |
func wait(options: Options) { | |
println("Waiting for \(options.timeout). Message: \(options.message)") | |
// TODO: wait for some condition | |
} | |
enum OptionKey { | |
case Timeout, Message | |
} | |
extension Options: DictionaryLiteralConvertible { | |
typealias Key = OptionKey | |
typealias Value = AnyObject | |
init(dictionaryLiteral elements: (Key, Value)...) { | |
for (key, value) in elements { | |
switch key { | |
case .Timeout: timeout = value as Double | |
case .Message: message = value as String | |
} | |
} | |
} | |
} | |
wait(Options(timeout: 0.1, message: "Custom timeout failed.")) | |
wait([.Timeout: 0.1]) | |
wait([.Timeout: 0.1, .Message: "Custom timeout failed."]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment