将对象转换为存储在变量中的类。

时间:2023-01-23 07:44:36

Is it possible to store a class type in a variable and later use that variable to cast an object to that class type?

是否可能将类类型存储在一个变量中,然后使用该变量将对象转换为该类类型?

I seem to be able to store the class type in a variable in the enum, but have been unable to us this variable to cast a new object to this type.

我似乎能够将类类型存储在enum中的一个变量中,但是无法让这个变量向该类型强制转换一个新对象。

I have an enum of subclassed types

我有一个子类类型的枚举

enum FruitType: Int {

    case orange, apple, lemon, lime

    var className: Fruit.Type {
        switch self {
            case.orange:
                return Orange.self
            case.apple:
                return Apple.self
            case.lemon:
                return Lemon.self
            case.lime:
                return Lime.self
        }
    }
}

I have subclasses for these game objects (Orange, Apple, Lemon & Lime) and they all inherit from "Fruit"

我有这些游戏对象的子类(橙子,苹果,柠檬和酸橙)它们都继承自"水果"

Later on I am trying to use that variable to cast an object to that type but it reports an error

稍后,我尝试使用该变量将对象转换为该类型,但它报告了一个错误

error "Use of undeclared type 'fruitType'

错误"使用未声明类型'水果类型'

func createFruit(fruitType: FruitType, name: String) {
    if let orange = self.childNode(withName: name) as? fruitType.className {
        self.orange = orange
    }
    etc...
}

I've also tried fruitType.className(self.childNode(withName: name)) with the same results

我也试过fruitType.className(自我。子节点(名称:name))具有相同的结果

I've looked into Generics but couldn't find anything that fit this situation.

我研究过泛型,但找不到适合这种情况的东西。

EDIT to show actual example

编辑以显示实际示例

The above question was simplified to make the question easier to state. The true nature of how I am handling this is that I am loading a SKS scene file as a SKReferenceNode based on fruitType and I need to cast that SKReferenceNode accordingly

上面的问题简化了,使问题更容易陈述。我如何处理这个问题的真正本质是,我将SKS场景文件加载为基于结果类型的SKReferenceNode,因此我需要相应地转换这个SKReferenceNode。

func loadFruit(fruitType: FruitType) {

    var fruit: Fruit!
    if let fruitFile = SKReferenceNode(filename: fruitType.fileName) {

        if let fruitNode = fruitFile.childNode(withName: fruitType.name) as? fruitType.className {
             fruitNode.setup()
        }
    }
}

1 个解决方案

#1


-2  

May be this will help you:

也许这会对你有所帮助:

Factory Method: The factory pattern is used to replace class constructors, abstracting the process of object generation so that the type of the object instantiated can be determined at run-time.

工厂方法:工厂模式用于替换类构造函数,抽象对象生成过程,以便在运行时确定实例化的对象的类型。

https://github.com/ochococo/Design-Patterns-In-Swift#-factory-method

https://github.com/ochococo/Design-Patterns-In-Swift工厂方法

#1


-2  

May be this will help you:

也许这会对你有所帮助:

Factory Method: The factory pattern is used to replace class constructors, abstracting the process of object generation so that the type of the object instantiated can be determined at run-time.

工厂方法:工厂模式用于替换类构造函数,抽象对象生成过程,以便在运行时确定实例化的对象的类型。

https://github.com/ochococo/Design-Patterns-In-Swift#-factory-method

https://github.com/ochococo/Design-Patterns-In-Swift工厂方法