0

Class A is writen in Objective C and have a custom init fucntion

@interface A ()

....

@end

@implementation A

- (id)customInitImplementedInA
{
    ... 
    return self;
}

Class B inherits from Class A and uses this custom init in the following way:

@interface B : A ()

....

@end

@implementation B
+(instancetype)instanceB{
    B *b = [[B alloc] customInitImplementedInA];
    ...
    return b;
}

Now I would like to create class C writen in Swift that inharits from A and uses the same init function. How do I do that?

class C: A {
  //How do I use customInitImplementedInA here?
}
9
  • In your actual customInitImplementedInA, the method name starts with init? Commented Aug 13, 2018 at 13:22
  • @OOPer Yes it is Commented Aug 13, 2018 at 13:26
  • Then no problem. Commented Aug 13, 2018 at 13:28
  • @OOPer So what is the answer? Commented Aug 13, 2018 at 13:34
  • 1
    It takes one parameter? Why don't you write the actual name in your question? Using pseudo name makes it complex. Commented Aug 13, 2018 at 13:43

2 Answers 2

1

You should be able to do this via super.methodname syntax.

class C: A {
    init() {
        super.customInitImplementedInA()
        // Any extra initialization for C goes here.
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

got an error for override: Initializer does not override a designated initializer from its superclass. Removed it and got an error for customInitImplementedInA: Value of type 'A' has no member 'customInitImplementedInA'
My mistake. If class A does not have a base init, then you would not need the override. Is customInitImplementedInA exposed via the interface or is it private? If it's private, you will not be able to access it.
It is indeed exposed via the interface. Can't imagine why this error arises
Does customInit have parameters and are you calling the initializer with proper swift syntax? When you do the super. does autocomplete provide anything relating to the methods from class A?
0

If you inherit your swift class from Obj-C than your swift clas will have obj-c init(). if you need to call it you can include it to your swift init.

 class C: A {

        var message: B?

        init(message: B) {
            super.customInitImplementedInA()

            self.message = message

        }
    }

2 Comments

What is the var message: B?
its just example of your custom init. there can be any var from your C class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.