@_dynamicReplacement(for: ) error in simulator

自古美人都是妖i 提交于 2020-04-18 05:35:57

问题


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:

  1. In pod B, use @_dynamicReplacement(for:) to swap methods in other pod components
  2. 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

  1. I use CocoaPods 1.9.1, Xcode 11.4 and Swift 5.2.
  2. Only use_frameworks! is used in the podfile
  3. Test and Test2 .podspec files with set static_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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!