surToTheW
surToTheW

Reputation: 832

Changing the implementation of Objective C method in Swift subclass

I am using an external library in my project. It is integrated via CocoaPods. I have access to the source code. The library is working well, but I need some modifications at some places in order for it to serve my purposes. It is written in Objective C. My project is in Swift. I need to change the implementation of one method in the library. The problem is it is in the .m file and uses a lot of stuff declared only in the .m file. For example:

-(NSString*)methodToChange
{
    NSArray<NSNumber*>* data = [self someInternalMethod:1];
    uint value = data[0].unsignedIntValue;
    return [self anotherInternalMethod:value];
}

I tried subclassing it like this:

class MySubclass : MySuperclassWithMethodToChange {
    override var methodToChange: String {
        //trying to use someInternalMethod and anotherInternalMethod
        //unsuccessfully because they are not visible 
    }
}

The internal methods are using and modifying properties from the .m file that are also not accessible. Is there any way to solve this?

Upvotes: 0

Views: 151

Answers (1)

Gereon
Gereon

Reputation: 17844

I would suggest forking the original library repository and making the necessary changes in your fork. You can then have your Podfile point to that. If your changes could be useful to others, make them in a way that doesn't break existing functionality and contribute them back to the library by opening a pull request.

If the original library gets updated later, you will have to merge those changes from the so-called "upstream" repository into yours. This does not happen automatically, so you'll have full control (and responsibility) over that process. See https://help.github.com/en/articles/syncing-a-fork for how this would look like.

Upvotes: 2

Related Questions