有没有办法为当前类类型动态创建实例?

时间:2019-04-01 05:59:42

标签: ios swift

假设我有10个以上的类,例如:class1,class2,class3,..等,每个类都有自己的功能。

我的问题是,由于我必须为每个类创建一个实例以使用其功能,我可以动态创建一个实例变量并分配类类型,而不是手动指定类型吗?

我的意思是这样的:

class class1 {
    static var instance = self
// or
    static var instance :type(of:self) {
        get {
            return type(of:self)
        }
    }
}
/* instance type will be "class1", through "instance" I can access to class public functions */

我当然尝试过这些想法,但是没有一个起作用。

有什么办法吗?

更新:

class Country {
      static var instance = Country()

      func do_something1 () { // do soething
      }
      func do_something2 () { // do soething
      }
      func do_something3 () { // do soething
      }
      func do_something4 () { // do soething
      }
      func do_somethingN () { // do soething
      }

  }

我想让instance根据存在的类来动态地键入它,而不是根据特定的类型来手动输入

1 个答案:

答案 0 :(得分:0)

您可以通过提供班级名称来获得班级,然后可以进行初始化。

mod_rewrite
相关问题