无法将 'MDLMaterialProperty?!' 类型的值分配给 'Int' 类型的值

Cannot assign a value of type 'MDLMaterialProperty?!' to a value of type 'Int'

我已经将我的 Xcode 6.4 项目更新为 Xcode 7,它有这个问题...

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?!' 类型的值分配给 'Int'

类型的值

它正在处理 Xcode 6.4...

这是 XCode 7 中的一个奇怪错误,它会导致在类型不匹配或变量未展开时弹出有关 "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)
        }
    }
}