问题
I use CocoaPods to manage my private components. Now I have a component Test
. The declaration of one of the classes is as follows:
open class Test {
open dynamic func test() {
print("test")
}
}
I have no problems with this component, it is already in my private repo
Now I have another component called Test2
. I want to use @_dynamicReplacement(for:)
in Test2
to exchange the test()
method in Test as follows:
public extension Test {
@_dynamicReplacement(for: test())
public func test2() {
print("test2")
}
}
Now I want to package Test2 as a pod component, like Test.
But when I execute pod spec lint
, it reports an error:
<unknown>:0: error: fatal error encountered during compilation; please file a bug report with your project and the crash log
<unknown>:0: note: unsupported relocation with subtraction expression, symbol '_$s9MBCRouter6RouterC23MBCNavigationController0cdB010NavigationORszrlE15navigationTest2yyF' can not be undefined in a subtraction expression
I found that this problem only appeared on x86_64
on i386
.
I tried to create a project that integrates both Test
and Test2
.
When I run it on a real machine, it works fine. But when I run it on the simulator, it reports an error!
When I placed the second piece of code above in the Test component, it worked without any problems.
So I think the following two conditions are needed to reproduce this bug:
- In
pod B
, use@_dynamicReplacement(for:)
to swap methods in other pod components - Run the project in the simulator
I found this problem, but I can't solve it, can anyone help to solve this problem? thank you very much!
Finally, I want to talk about my development environment
- I use CocoaPods 1.9.1, Xcode 11.4 and Swift 5.2.
- Only
use_frameworks!
is used in the podfile Test
andTest2
.podspec
files with setstatic_framework = true
回答1:
To build for Simulator you need add @objc
attribute to a function declaration e.g:
open class Test {
open @objc dynamic func test() {
print("test")
}
}
UPDATE: More common solution (included Generics) - set Compilation Mode to Incremental in Build Settings and in this case you don't need to use @objc
at all for the Simulator.
来源:https://stackoverflow.com/questions/60986318/dynamicreplacementfor-error-in-simulator