2015-05-18 50 views
-2

給出錯誤:類型「字符串」不符合協議「IntervalType」這個swift switch語句有什麼問題?

var value = "MELBOURNE" 

func citycode(value:String) -> String{     
    switch (value){    
     case "MELBOURNE": 
      return "MEL" 
     case "TOKYO 
      return "TOK" 

    default : 
     return "MEL" 
    } 
} 
+1

你的「東京」的情況是不完整的......就是這樣。 – Eendje

+0

關閉你的「TOKYO」字符串與另一個「和: – Chackle

+1

當我把你的代碼粘貼到一個Xcode項目中,然後我得到(非常清晰)的錯誤消息」未終止的字符串「 –

回答

0
var value = "MELBOURNE" 

func citycode(value: String) -> String { 
    switch (value){ 
    case "MELBOURNE": return "MEL" 
    case "TOKYO" : return "TOK" 
    default: return "MEL" 
    } 
} 

citycode(value) // Prints "MEL" 

你的情況爲「TOKYO」是不完整的......多數民衆贊成。

+0

另外,不需要用parens包裝」value「 :'開關值{...}'。 – Moritz