Swift defined convenience init is not accessible in objective-c

384 views Asked by At

I have a swift class which is a subclass of NSObject. The problem is when I try to define init in an extension. Consider this sample class:

public class Person: NSObject {
  public init(name: String) {
    self.name = name 
  }
}

So when I define init in an extension like this in my main app:

extension Person {
  @objc public convenience override init() {
    self.init(name: "test")
  }
}

and try to create object in objective-c like this:

[[Person alloc] init];

it gives me this error: 'init' is unavailable and it forces me to use initWithName

but when I define init inside the class and not extension, everything works:

public class Person: NSObject {
  public init(name: String) {
    self.name = name 
  }

  @objc public convenience override init() {
    self.init(name: "test")
  }
}

I'm sure the extension is available to my obj-c class and also have imported "ProjectTarget-Swift.h" to my obj-c file but have no idea why this doesn't work, anyone can help? Thanks

0

There are 0 answers