ExpressibleByStringLiteral

时间:2017-07-18 15:23:57

标签: swift enumeration string-literals

为什么(让key1:Keyword =“if”)顺利但(让key2:Keyword = String(“if”))出错?

public enum Keyword: String, ExpressibleByStringLiteral, ExpressibleByUnicodeScalarLiteral, ExpressibleByExtendedGraphemeClusterLiteral {    
case `if` = "if"
case `else` = "else"
public init(stringLiteral:String){
    self.init(rawValue: stringLiteral)!
}
public init(unicodeScalarLiteral value: String) {
    self.init(rawValue: value)!
}
public init(extendedGraphemeClusterLiteral value: String) {
    self.init(rawValue: value)!
}
}

let key1:Keyword = "if" //going well
let key2:Keyword = String("if") //error reported

0 个答案:

没有答案