I have the code below, which the compiler is happy with:
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
}
But once I define the item
type in the block signature, I get an error on the return line:
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
}
It says Value of type 'Any?' has no member 'ColorCode'
. How can I resolve this? Why is it still using the default Any type provided by the block?