2016-09-21 87 views
2

我正在使用enumtuple以及枚舉大小寫的值。我從[String:String]常量獲取值時遇到了問題。從常量獲取值時出錯:對成員'下標'的歧義引用

我不知道如何解決它,它必須是一個陷阱,但我不知道在哪裏,因爲key肯定是串:

enum DictTypes : String { 
     case settings 
     case options 
     case locations 
    } 
    enum FileTypes : String { 
     case json 
     case pList 
    } 

    func getCodebookUrlComponent() -> String 
    { 
     var FileSpecs: (
       dictType: DictTypes, 
       fileType: FileTypes, 
       redownload: Bool 
      ) = (.settings, .json, true) 

     let codebooks = [ 
      "settings" : "settings", 
      "options" : "options" 
     ] 

     let key = self.FileSpecs.dictType // settings or options 

     if let urlComponent = codebooks[key] { 

      return urlComponent 
     } 

     return "" 
    } 

此行if let urlComponent = codebooks[key]配備了一個錯誤:

Ambiguous reference to member 'subscript'

回答

3

你應該使用.rawValue這種情況:

if let urlComponent = codebooks[key.rawValue]{ 
    return urlComponent 
} 

發生此問題的原因是let key = self.FileSpecs.dictType在此行中收到的密鑰即FileSpecs類型。在Array中實施的subscript將不符合該值類型。

rawValue你的情況返回你分配給你的字符串值是enum

+0

謝謝,我我就不會得到它 – GiorgioE

1

因爲從枚舉值的情況下肯定是串,我就喜歡這類型的:

let key = FileSpecs.dictType.rawValue // "settings" or "options" 

let key = String(describing: FileSpecs.dictType) 

return codebooks[key]! 
+0

它甚至更短,謝謝 – GiorgioE

相關問題