Ios 无法指定类型为';材料属性';设置为类型为';Int';
我已经将我的Xcode 6.4项目更新为Xcode 7,它有这个问题Ios 无法指定类型为';材料属性';设置为类型为';Int';,ios,swift2,xcode7,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()){
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?!”的错误 尝试此代码(固定在两行中):
回答得好!此外,当您有复杂的对象层次结构时,可能会遇到这种情况。你必须大声叫喊并施展树上的每一步。如果你试着用花哨的方式组合行,每次都会抛出同样的错误并失败。i、 e.如果buh[“foo”]![“酒吧”]!作为?词典。。将抛出错误。所以你必须为每一步声明变量。。。瘸的仅在7.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)
}
}
}