1

I have init() method on objc class.

A swift class subclasses the objc class and tries to call super.init()

it's an error because init() is not an designated initializer for MyObjcViewcontroller.

@interface MyObjcViewController: UIViewController {
 }

- (id) init;

@end

 @implementation MyObjcViewController

 - (id) init {
     self = [super init];
     if (self) {
     }
     return self;
 }

 @end


 @objc class MySwiftViewController: MyObjcViewController {

     override init() {
         super.init()            // error
     }

 }
eugene
  • 39,839
  • 68
  • 255
  • 489
  • `MySwiftViewcontroller::init()` looks more like C++ than Objective-C or Swift ... – Martin R Nov 06 '15 at 07:18
  • oh I just tried to show the pesudo code.. yeah I'll fix it. – eugene Nov 06 '15 at 07:19
  • Perhaps it helps to decorate the Objective-C method with NS_DESIGNATED_INITIALIZER, compare http://stackoverflow.com/questions/26185239/ios-designated-initializers-using-ns-designated-initializer. – Martin R Nov 06 '15 at 07:20
  • Yes it does help. put that thing after the declaration of the parent class. I also had to add `override` to the subclass method. I could accept if you post it as an answer. – eugene Nov 06 '15 at 07:25
  • Actually I cannot reproduce your problem. Overriding an Objective-C init method in Swift compiles without problems for me (if `override` is specified) even without NS_DESIGNATED_INITIALIZER. – Can you provide a minimal self-contained example (with .h, .m and Swift code)? – Perhaps the ObjC class already has (another) designated initializer? – Martin R Nov 06 '15 at 08:05
  • well, I guess it's due to the fact that UIViewController has `initWithNibName:bundle:` as the designated initializer. http://stackoverflow.com/questions/24095037/why-cant-i-call-the-default-super-init-on-uiviewcontroller-in-swift Try to subclass UIViewController in objc and subclass that in swift – eugene Nov 06 '15 at 08:11
  • Does your ObjC class declare an init method (in the .h file)? Again, a (minimal) concrete example would be helpful to avoid misunderstandings or guessing. – Martin R Nov 06 '15 at 08:30
  • Strange. I have copied your code into an Xcode 7.1 project. The only thing that Xcode complained about was a missing `required init?(coder aDecoder: NSCoder)` method in MySwiftViewController. – Martin R Nov 06 '15 at 08:43
  • hmm strange.. I'm on Xcode 7.0.1 if that makes a difference. :( – eugene Nov 06 '15 at 08:48

1 Answers1

0

your trying to put objective-c and swift into the same line of code. if you want to do that, you should use a bridge so the files compile without interference between compilers. if this doesn't answer your question, please tell me.

Camden
  • 283
  • 2
  • 15