我尝试创建通用函数
func importArray<T: ImportableUniqueObject>(from exercisesDict: [[String: Any]], transaction: BaseDataTransaction) -> [T] {
if let managedObject = try? transaction.fetchOne(From<T>()){
transaction.delete(managedObject)
}
let managedObjects = try! transaction.importUniqueObjects(
Into<T>(),
sourceArray: jsonObjects)
return managedObjects
}
所以第一部分效果很好:
if let managedObject = try? transaction.fetchOne(From<T>()){
,但是秒不起作用:
let managedObjects = try! transaction.importUniqueObjects(
Into<T>(),
sourceArray: jsonObjects)
编译器说
Cannot convert value of type 'Into<T>' to expected argument type 'Into<_>'
这是func的构造方式:
public func importUniqueObjects<D: ImportableUniqueObject, S: Sequence>(
_ into: Into<D>,
sourceArray: S,
preProcess: @escaping (_ mapping: [D.UniqueIDType: D.ImportSource]) throws -> [D.UniqueIDType: D.ImportSource] = { $0 }) throws -> [D] where S.Iterator.Element == D.ImportSource {
答案 0 :(得分:1)
那是编译器错误。 Xcode 10发布时出现了相同的问题。 使您的方法适应以下要求:
func importArray<T: ImportableUniqueObject>(from exercisesDict: [[String: Any]], transaction: BaseDataTransaction) -> [T] where T.ImportSource == [String: Any] {
let managedObjects = try? transaction.importUniqueObjects(Into<T>(), sourceArray: jsonObjects)
}
尽管我建议在导入时不要强行尝试。