swiftgeneric-programmingcorestore

Swift generic func cannot convert value of type to expected argument type


I try to create generic func

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

    }

So first part works good:

if let managedObject = try? transaction.fetchOne(From<T>()){

,but second does not work:

let managedObjects = try! transaction.importUniqueObjects(
        Into<T>(),
        sourceArray: jsonObjects)

Compiler says

Cannot convert value of type 'Into<T>' to expected argument type 'Into<_>'

This is how func is constructed:

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 {

Solution

  • That's a compiler bug. Had the same issue when Xcode 10 came out. Adapt your method to the following:

    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)
    }
    

    Though I recommend do not do force-try when importing.

    Also see: https://bugs.swift.org/browse/SR-8945