我有下面的代码,编译器对此很满意:
func CheckPaintExists(colorCode : String, applicationCode : String) {
let checkRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Paint")
checkRequest.predicate = NSPredicate(block: { (item, bindings) -> Bool in
return (item as! Paint).ColorCode == colorCode
&& (item as! Paint).ApplicationCode == applicationCode
})
checkRequest.includesSubentities = false;
//managedContext.count(for: ...)do further stuff
}
但是一旦我item
在块签名中定义了类型,我就会在返回行上得到一个错误:
func CheckPaintExists2(colorCode : String, applicationCode : String) {
let checkRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Paint")
checkRequest.predicate = NSPredicate(block: { (item : Paint?, bindings : NSDictionary?) -> Bool in
return item.ColorCode == colorCode //*Value of type 'Any?' has no member 'ColorCode'
&& item.ApplicationCode == applicationCode
})
checkRequest.includesSubentities = false;
//managedContext.count(for: ...)do further stuff
}
它说Value of type 'Any?' has no member 'ColorCode'
。我该如何解决这个问题?为什么它仍然使用块提供的默认 Any 类型?