if 的条件式可以是一个实现了 BooleanType 协议的Class
结合 StringLiteralConvertible 协议 实现一个可以用字符串定义的条件式(对实现多类型条件式判定方法的统一)
class Condition: BooleanType,StringLiteralConvertible { //对BooleanType协议的实现 var result: Bool = false var boolValue: Bool { get { return result } } init() { } //对StringLiteralConvertible协议的实现 typealias ExtendedGraphemeClusterLiteralType = String typealias UnicodeScalarLiteralType = String required convenience init(stringLiteral value: StringLiteralType){ self.init() if value == "" || value == "0" { result = false } else { result = true } } required convenience init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType){ self.init() } required convenience init(unicodeScalarLiteral value: UnicodeScalarLiteralType){ self.init() } } //可以用一个文字串来定义一个条件式 var condition: Condition = "0" if condition { println("这是真的") } else { println("这是假的") }