Swift:创建一个将类类型作为参数的工厂函数,并输出该类的构造函数 [英] Swift: Creating a factory function that takes a class type as a parameter, and outputs a constructor of that class
问题描述
我想创建一个工厂函数,它接受一个类类型并返回一个构造函数,以便我可以使用该构造函数来创建该类的实例。
$ b $假设我有两个类,Apple和Orange,它们都是Fruit的子类。他们需要用
unknownNumber
进行初始化,我稍后才会知道。 class Apple:Fruit {
init(unknownNumber:Int){
...
}
}
class Orange:Fruit {
init(unknownNumber:Int){
...
}
}
b $ b
我想创建一个接受Class类型的工厂函数,以便我以后可以调用这个函数,并用UnknownNumber初始化Fruit的特定子类。
//概念:
func makeFruit(typeOfFruit) - > (Int)→>水果{
return {(unknownNumber:Int) - >水果在
中返回typeOfFruit(unknownNumber)
}
}
要创建一个orangeFactory,我可以这样做:
let orangeFactory = makeFruit(Orange)
// then after a later time when I have the unknown number
let orangeInstance = orangeFactory(unknownNumber)
我知道可以简单地使 unknownNumber
一个惰性变量,但在我的具体情况下 unknownNumber
不仅仅是一个数字,它涉及其他进程,所以我只想创建对象,当我有一切可用,保持结构简单。
这样的东西可能在Swift?我一直在研究一段时间在线,似乎找不到任何直接的答案。任何帮助将非常感谢!
让我们倒退。在您的 makeFruit
函数中,您需要将 typeOfFruit
参数声明为 Fruit
超类并明确引用初始化器:
func makeFruit(typeOfFruit:Fruit.Type) > (Int)→>水果{
return {(unknownNumber:Int) - > Fruit in
return typeOfFruit.init(unknownNumber:unknownNumber)
}
}
$ b b
您只能在元类型上访问必需的
初始化器,因此 init
需要标记为:
class Fruit {
required init(unknownNumber:Int){
// ...
}
}
其余应该工作:
let orangeMaker = makeFruit(Orange.self)
pre>
let tenOranges = orangeMaker(10)
I'd like to create a factory function that takes in an class type and returns a constructor, so that I can use that constructor to create an instance of that class later.
Imagine I have two classes, Apple and Orange, which are both subclasses of Fruit. They need to be initialized with an
unknownNumber
which I will only know about later.class Apple: Fruit { init(unknownNumber: Int) { ... } } class Orange: Fruit { init(unknownNumber: Int) { ... } }
I'd like to create a factory function that takes in a Class type, so that I can later call this function and initialize the specific subclass of Fruit, with the unknownNumber.
//concept: func makeFruit(typeOfFruit) -> (Int) -> Fruit { return { (unknownNumber: Int) -> Fruit in return typeOfFruit(unknownNumber) } }
To create an orangeFactory, then, I can do:
let orangeFactory = makeFruit(Orange) // then at a later time when I have the unknown number let orangeInstance = orangeFactory(unknownNumber)
I am aware of the option of simply making the
unknownNumber
a lazy variable, but in my specific case theunknownNumber
is not just a number and it involves other processes, so I'd like to only create the object when I have everything available, to keep the structure simple.Is something like this possible in Swift? I've been researching for a while online and couldn't seem to find any direct answers. Any help would be greatly appreciated!
解决方案Let's work backwards. In your
makeFruit
function, you'll need to declare thetypeOfFruit
parameter as a metatype of yourFruit
superclass and explicitly reference the initializer:func makeFruit(typeOfFruit: Fruit.Type) -> (Int) -> Fruit { return { (unknownNumber: Int) -> Fruit in return typeOfFruit.init(unknownNumber: unknownNumber) } }
You can only access
required
initializers on a metatype, so thatinit
needs to be marked as such:class Fruit { required init(unknownNumber: Int) { // ... } }
The rest should just work:
let orangeMaker = makeFruit(Orange.self) let tenOranges = orangeMaker(10)
这篇关于Swift:创建一个将类类型作为参数的工厂函数,并输出该类的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!