Use Type Erasure return Generic Type in a function with Swift (Cannot convert return expression of type…)

前端 未结 3 745
一生所求
一生所求 2021-01-25 01:31

I have a problem with generics in swift. Let\'s expose my code.


protocol FooProtocol {
    associatedtype T
}

protocol Fooable { }
extension Int : Fooable { }
         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-25 01:59

    Is the following what you tried to achieve? (compiled & tested with Xcode 11.4)

    func createOne() -> some FooProtocol {
        let anyFoo = AnyFoo(p: FooImpClass())
        return anyFoo
    }
    

提交回复
热议问题