我希望一个通用函数返回我任何实体的记录
我已经创建了一个通用的提取请求,但似乎无法返回结果。
func syncGetLocalChanges(_ entity: String, _ lastSyncDT: Date) -> [AnyClass] {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
let request = NSFetchRequest<NSFetchRequestResult>(entityName: entity)
request.predicate = NSPredicate(format: "modUTC >= %@", lastSyncDT as CVarArg)
do {
let changedRecords = try context.fetch(request)
return changedRecords // <--- *** ERROR here: Cannot convert return expression of type '[Any]' to return type '[AnyClass]' (aka 'Array')
} catch {
print("Error in \(#function): \(error)")
}
}
return语句有错误 无法将类型“ [Any]”的返回表达式转换为类型“ [AnyClass]”(也称为“数组”)
我可以仅根据实体名称的字符串输入返回实体记录吗?