Swift Framework does not include symbols from extensions to generic structs

后端 未结 2 879
余生分开走
余生分开走 2020-12-09 17:41

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 generi

相关标签:
2条回答
  • 2020-12-09 18:15

    In case you are looking for a temporary fix, you can wrap the extension in a class method:

    // In your framework
    public class OptionalOperator {
        public class func or<T>(optional:Optional<T>,defaultValue:T) ->T {
            return optional.or(defaultValue)
        }
    }
    
    // Outside the framework
    var maybeText:String?
    let text = OptionalOperator.or(maybeText, defaultValue: "Apple, please fix this")
    

    Of course, this is not ideal and defeats the purpose of extensions. So if you plan on calling this method frequently, we could overload/define an operator.

    // In your framework
    infix operator ||| {}
    
    public func |||<T>(left:Optional<T>, right:T) -> T {
        return left.or(right)
    }
    
    // Outside the framework
    var maybeText:String?
    let text = maybeText ||| "Apple, please fix this"
    

    In my case, I have multiple applications using the framework, so I'd like to keep the method implementation inside the framework. However, overloading an operator (or just using a global function) would be awkward, so I have to go with the first option until that bug is fixed.

    Hope this helps.

    UPDATE

    Funny thing is that Swift already has an operator for that (??).

    var maybeText:String?
    let text = maybeText ?? "Nice!"
    

    It's called - Nil Coalescing Operator

    0 讨论(0)
  • 2020-12-09 18:23

    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.

    0 讨论(0)
提交回复
热议问题