泛型协议在Swift中的类型

时间:2022-09-11 19:08:43

How I can create a protocol variable. My goal is a protocol will be have a function with a generic type and I'm using associatedtype which is accessing to Class and the function will be return generic type. Example declaration below:

我如何创建协议变量。我的目标是协议将具有泛型类型的函数,并且我正在使用正在访问类的关联类型,并且该函数将返回泛型类型。以下示例声明:

public protocol ComponentFactory {
   associatedtype T // A class and that class can be inherit from another so I need define generic type here
   func create() -> T
}

I want to declare a variable for this protocol like that:

我想为这个协议声明一个变量:

fileprivate var mComponentFactoryMap = Dictionary<String, ComponentFactory>()

At this line I receive a error: Protocol 'ComponentFactory' can only be used as a generic constraint because it has Self or associated type requirements

在这一行,我收到一个错误:协议'ComponentFactory'只能用作通用约束,因为它具有Self或相关的类型要求

I see from Android, actually from kotlin they have a declare for interface like:

我从Android看到,实际上来自kotlin他们有一个声明接口,如:

private val mComponentFactoryMap = mutableMapOf<String, ComponentFactory<*>>()

Any guys can help me, how I can declare this from Swift?

任何人都可以帮助我,我怎么能从Swift声明这个?

1 个解决方案

#1


0  

I've solved this from few months ago with descriptions below. Please check it and give me another solution if you have.

几个月前我已经用下面的描述解决了这个问题。如果有,请检查并给我另一种解决方案。

Firstly, make some change for Protocol. At associatedtype T should be change to associatedtype Component and Component is a class which will be inherited from another class (important step).

首先,对Protocol进行一些更改。在关联类型T应该更改为关联类型组件,组件是一个将从另一个类继承的类(重要步骤)。

public protocol ProComponentFactory {
    associatedtype Component
    func create() -> Component?
} 

Second, I will make a Generic Struct with inheritance from ProComponentFactory:

其次,我将从ProComponentFactory继承一个Generic Struct:

public struct ComponentFactory<T>: ProComponentFactory {
    public typealias Component = T
    public func create() -> T? { return T.self as? T }
}

Well done, for now you can define a variable as I example in my question above:

干得好,现在你可以在我上面的问题中定义一个变量作为例子:

fileprivate var mComponentFactoryMap = Dictionary<String, ComponentFactory<Component>>()

As well for any class was inherit from Component and variable mComponentFactoryMap can using extension inside.

同样,任何类都继承自Component,而变量mComponentFactoryMap可以在内部使用扩展。

#1


0  

I've solved this from few months ago with descriptions below. Please check it and give me another solution if you have.

几个月前我已经用下面的描述解决了这个问题。如果有,请检查并给我另一种解决方案。

Firstly, make some change for Protocol. At associatedtype T should be change to associatedtype Component and Component is a class which will be inherited from another class (important step).

首先,对Protocol进行一些更改。在关联类型T应该更改为关联类型组件,组件是一个将从另一个类继承的类(重要步骤)。

public protocol ProComponentFactory {
    associatedtype Component
    func create() -> Component?
} 

Second, I will make a Generic Struct with inheritance from ProComponentFactory:

其次,我将从ProComponentFactory继承一个Generic Struct:

public struct ComponentFactory<T>: ProComponentFactory {
    public typealias Component = T
    public func create() -> T? { return T.self as? T }
}

Well done, for now you can define a variable as I example in my question above:

干得好,现在你可以在我上面的问题中定义一个变量作为例子:

fileprivate var mComponentFactoryMap = Dictionary<String, ComponentFactory<Component>>()

As well for any class was inherit from Component and variable mComponentFactoryMap can using extension inside.

同样,任何类都继承自Component,而变量mComponentFactoryMap可以在内部使用扩展。