0

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?

FrankZp
  • 2,022
  • 3
  • 28
  • 41
  • 3
    Fork the project and modify the code. – jnpdx Jan 28 '22 at 22:10
  • 1
    If it's a public method, then you can wrap the framework in your own that forwards every method exact that one, and does what you want for that one (this won't change the internal behavior, though). If it's an internal method, then by the time you're calling it, it may not even exist. The compiler is free to inline it. Talk with whoever supports the package, and ask for a feature, or fork and add the feature yourself. (But the short answer to your question is "you can't in any stable and reliable way, and that's more or less on purpose.") – Rob Napier Jan 28 '22 at 22:22
  • @jnpdx I understand that would be a possibility, but requires that I keep the fork in sync with the original repo, which means some overhead on my side (https://stackoverflow.com/a/7244456) – FrankZp Jan 28 '22 at 22:29
  • 1
    @FrankZp Yes, any solution that involves replacing a method like this will have overhead. With the fork method, you'd have the advantage of seeing merge conflicts when moving in new commits if something conflicts. – jnpdx Jan 28 '22 at 22:30
  • @RobNapier That sounds interesting. Can you please be a bit more specific with that? You mean create an own framework? And what do you mean with forward every method? – FrankZp Jan 28 '22 at 22:30
  • I mean create a framework that imports the framework you want to modify. Write your own method for every public method you want to call. Forward every method except the ones you want to change. It's tedious. I don't mean there's some magical "just fix it" solution. I mean "it's just code; you can always make it do whatever you want if you write enough of it." (Fundamentally, though, this approach is "composition" rather than "inheritance." Hold a copy of the thing you want to modify, and modify it by calling it differently, or not at all.) – Rob Napier Jan 28 '22 at 22:35
  • @FrankZp Have you found applicable solution for this issue? Because I have the same. – Konstantin.Efimenko Jun 07 '23 at 11:14

1 Answers1

0

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

Pauli
  • 343
  • 1
  • 4
  • 17