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

时间:2015-09-19 14:31:19

标签: ios swift2 xcode7

我已将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 并说:不能指定类型&MDHMaterialProperty类型的值?!'值为' Int'

它正在开发Xcode 6.4 ......

1 个答案:

答案 0 :(得分:1)

这是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)
        }
    }
}