Home > Software engineering >  Swift: Replace a method of a class from a SPM package
Swift: Replace a method of a class from a SPM package

Time:01-30

I use an open source package via SPM, but I would like to change the details within one specific func originalMethod() in this package.

As the code of SPM packages is not editable, I can't change the code. I cannot swizzle the method with Swift 5.1 @_dynamicReplacement(for: originalMethod) as the original method is not marked as dynamic.

What other options do I have besides copying the package manually into the project?

CodePudding user response:

Beside the options mentioned in the comments:

  • fork the repository (by jnpdx)
  • wrap the framework in your own framework (by Rob Napier)

You could use the C library mach_override to override the implementation of that method. It is highly unsafe and I wouldn’t recommend this for any productive application.

Here is a code example: https://github.com/lombax85/SwiftOverride

  •  Tags:  
  • Related