在Swift中创建运行时已知的类实例

时间:2021-10-12 13:16:29

A picture is worth a thousand words, how to rewrite this code from Objective-C to Swift?

一张图片胜过千言万语,如何将这段代码从Objective-C重写为Swift?

- (id) instanceOfClass: (Class) class withInitializer: (SEL) initializerSelector withObject: (id) object {
    id obj = nil;
    if([class instancesRespondToSelector:initializerSelector]) {
        obj = [[class alloc] performSelector:initializerSelector
                                  withObject:object];
    }
    return obj;
}

id myViewController = [self instanceOfClass:[ViewController class]
                              withInitializer:@selector(initWithObject:)
                                   withObject:@"super-string!"];
NSLog(@"%@", myViewController);

3 个解决方案

#1


2  

This cannot be done purely in Swift. You can only do this by creating the "class instance by name creator" in Objective C and calling this code from Swift.

这不能仅仅在Swift中完成。您只能通过在Objective C中创建“按名称创建者的类实例”并从Swift调用此代码来完成此操作。

For more information you can read this article. http://ijoshsmith.com/2014/06/05/instantiating-classes-by-name-in-swift/

有关更多信息,请阅读本文。 http://ijoshsmith.com/2014/06/05/instantiating-classes-by-name-in-swift/

And check out this github repo https://github.com/ijoshsmith/swift-factory

并查看这个github repo https://github.com/ijoshsmith/swift-factory

#2


0  

If you can make your classes subclasses of a common superclass you can do this:

如果您可以使您的类成为公共超类的子类,则可以执行以下操作:

class C {
  var typ:String
  init() {
    self.typ = "C"
  }
  class func newInst() -> C {
    return C()
  }
}

class C1 : C {
  override init() {
    super.init()
    self.typ = "C1"
  }
  override class func newInst() -> C1 {
    return C1()
  }
}

class C2 : C {
  override init() {
    super.init()
    self.typ = "C2"
  }
  override class func newInst() -> C2 {
    return C2()
  }
}

var CL:C.Type = C1.self
CL = C2.self
var inst = CL.newInst()

inst.typ

If not then you can use a closure or block to create the instance and place those in a dictionary by name.

如果没有,那么您可以使用闭包或块来创建实例,并按名称将它们放在字典中。

#3


0  

See matt's excellent answer here. Basically with @objc you can mimic the dynamic creation of instances.

在这里看到matt的优秀答案。基本上使用@objc,您可以模仿动态创建实例。

#1


2  

This cannot be done purely in Swift. You can only do this by creating the "class instance by name creator" in Objective C and calling this code from Swift.

这不能仅仅在Swift中完成。您只能通过在Objective C中创建“按名称创建者的类实例”并从Swift调用此代码来完成此操作。

For more information you can read this article. http://ijoshsmith.com/2014/06/05/instantiating-classes-by-name-in-swift/

有关更多信息,请阅读本文。 http://ijoshsmith.com/2014/06/05/instantiating-classes-by-name-in-swift/

And check out this github repo https://github.com/ijoshsmith/swift-factory

并查看这个github repo https://github.com/ijoshsmith/swift-factory

#2


0  

If you can make your classes subclasses of a common superclass you can do this:

如果您可以使您的类成为公共超类的子类,则可以执行以下操作:

class C {
  var typ:String
  init() {
    self.typ = "C"
  }
  class func newInst() -> C {
    return C()
  }
}

class C1 : C {
  override init() {
    super.init()
    self.typ = "C1"
  }
  override class func newInst() -> C1 {
    return C1()
  }
}

class C2 : C {
  override init() {
    super.init()
    self.typ = "C2"
  }
  override class func newInst() -> C2 {
    return C2()
  }
}

var CL:C.Type = C1.self
CL = C2.self
var inst = CL.newInst()

inst.typ

If not then you can use a closure or block to create the instance and place those in a dictionary by name.

如果没有,那么您可以使用闭包或块来创建实例,并按名称将它们放在字典中。

#3


0  

See matt's excellent answer here. Basically with @objc you can mimic the dynamic creation of instances.

在这里看到matt的优秀答案。基本上使用@objc,您可以模仿动态创建实例。