Name collisions for extension methods from different frameworks

前端 未结 3 1158
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 06:36

As a test, I created two frameworks. Both frameworks contain this extension:

public extension UIDevice {
    var extraInfo: UIDeviceExtraInfo {
        return U         


        
3条回答
  •  太阳男子
    2021-02-04 07:07

    In this specific case, you can disambiguate extraInfo prop by explicilty specifying expected type:

    import FW1
    import FW2
    
    let fw1Info = UIDevice.currentDevice().extraInfo as FW1.UIDeviceExtraInfo
    let fw2Info = UIDevice.currentDevice().extraInfo as FW2.UIDeviceExtraInfo
    print(fw1Info.prop) // -> Device1
    print(fw2Info.prop) // -> Device2
    

    But, when the method/prop return the same type:

    // FW1
    public extension UIDevice {
        var myInfo: String { return "Device1" }
    }
    
    // FW2
    public extension UIDevice {
        var myInfo: String { return "Device2" }
    }
    
    // App
    import FW1
    
    print(UIDevice.currentDevice().myInfo) // -> ???
    

    There is no way to disambiguate them. And regardless the application code imports which Framework, it seems, the actual called implementation depends on the order in Linked Frameworks and Libraries in "First come, first served" manner. But, as far as I know, this behavior is not guaranteed.

提交回复
热议问题