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

≯℡__Kan透↙ 提交于 2019-12-11 14:58:28

问题


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 {

回答1:


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



来源:https://stackoverflow.com/questions/55976440/swift-generic-func-cannot-convert-value-of-type-to-expected-argument-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!