2015-09-19 64 views
3

我已經更新了我的Xcode 6.4項目到Xcode的7,它有這個問題...無法指定'MDLMaterialProperty ?!'類型的值以類型的值「詮釋」

class func preparationForSave(text_country: NSDictionary){ 
    let dataArray = text_country["countries"] as! NSArray; 

    for item in dataArray { 
     var it: Int = 0 
     if (item["parentId"] == NSNull()){ 
      it = 0 
     }else{ 
      it = item["parentId"] 
     } 
     Country.saveCountry(item["id"] as! Int, title: item["title"] as! String, parentId: it) 
    } 
} 

它的主要錯誤在這裏:item["id"] as! Int 並說:不能分配的值鍵入'MDLMaterialProperty ?!'以類型的值「詮釋」

這是工作在Xcode的6.4 ...

回答

1

這是在7 XCode的一個奇怪的錯誤,這會導致約錯誤「MDLMaterialProperty?!」在類型不匹配或變量未解包時彈出。

試試這個代碼(固定在2號線):

class A { 
    class func preparationForSave(text_country: NSDictionary){ 
     let dataArray = text_country["countries"] as! NSArray; 

     for item in dataArray { 
      var it: Int = 0 
      if (item["parentId"] == nil) { // item["parentId"] is of type X? - compare it with nil 
       it = 0 
      }else{ 
       it = item["parentId"] as! Int // note that we're force converting to Int (might cause runtime error), better use: if it = item["parentId"] as? Int { ....} else { .. handle error .. } 
      } 
      Country.saveCountry(item["id"] as! Int, title: item["title"] as! String, parentId: it) 
     } 
    } 
} 
+1

好答案!另外,當你有複雜的對象層次結構時,你可能會遇到這個問題。你必須調出並在樹中執行每一步。如果你想要看到並結合線條,它會拋出同樣的錯誤,每次都會失敗。即如果buh [「foo」]![「bar」]!如? NSDictionary ..會拋出錯誤。所以你必須爲每個步驟聲明變量......跛腳。在7.2中只能看到這個。 –

相關問題