Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

ios - Swift Framework does not include symbols from extensions to generic structs

I am having trouble linking my framework with code that takes advantage of that framework. Specifically, the linker isn't able to find the symbols for extensions for generics structs.

This is what one of the extensions looks like for Optional:

extension Optional {
    /// Unwrap the value returning 'defaultValue' if the value is currently nil
    func or(defaultValue: T) -> T {
        switch(self) {
            case .None:
                return defaultValue
            case .Some(let value):
                return value
        }
    }
}

This method works great in a playground or in an app if the code is compiled within the main part of the app. However, when I try to compile this into a Framework, apps (and even the tests for the framework) produce the following linker error:

Undefined symbols for architecture i386: "__TFSq2orU__fGSqQ__FQQ", referenced from: __TFC18SwiftPlusPlusTests27Optional_SwiftPlusPlusTests13testOrWithNilfS0_FT_T_ in Optional+SwiftPlusPlusTests.o

Similar methods like the one following, link fine (notice, it is not on a generic)

extension String {
    /// Returns a string by repeating it 'times' times
    func repeat(times: Int) -> String {
        var result = ""
        for i in 0..times {
            result += self
        }
        return result
    }
}

There are two other extensions within my repository on github: SwiftPlusPlus that also do not link (both on generic strucs). You will reproduce the errors if you pull the latest commit, build the framework, and then try to run the unit tests.

So far I have tried to run "strings" on the outputted framework and intermediate files and I do not see the symbols for these extensions but I do see the symbols for the repeat method extension on String. So it doesn't even seem to be compiling them into the library.

Does anyone know why the symbols are not defined in the framework?

Edit

  • Here is a link to my Optional Extension
  • Here is a link to the test file that causes the linker error when trying to compile the test target
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I posted on the Apple Developer forums and an Apple employee responded that this is a known bug.

It looks like the compiler gets the mangled symbol names of methods in generic extensions wrong when they live in a different framework.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...