Swift 扩展:两个模块中的相同扩展功能 [英] Swift Extension: same extension function in two Modules

查看:117
本文介绍了Swift 扩展:两个模块中的相同扩展功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为 SwiftKit 的框架,它有一个名为 someClassMethod 的 UIView 扩展类方法和一个名为 someProperty 的属性:

Say I have a Framework called SwiftKit, which has a UIView's extension class method named someClassMethod and a property named someProperty within it:

// SwiftKit
public extension UIView {
    class func someClassMethod() {
        print("someClassMethod from Swift Kit")
    }

    var someProperty: Double {
        print("someProperty from Swift Kit")
        return 0
    }
}

我还有一个叫 SwiftFoundation 的框架,它里面还有一个 UIView 的扩展类方法叫 someClassMethod 和一个叫 someProperty 的属性:

And I also have a Framework called SwiftFoundation, which also has a UIView's extension class method named someClassMethod and a property named someProperty within it:

// SwiftFoundation
public extension UIView {
    class func someClassMethod() {
        print("someClassMethod from Swift Foundation")
    }

    var someProperty: Double {
        print("someProperty from Swift Foundation")
        return 0
    }
}

然后我创建了一个项目来介绍这些框架,事情是,如果我将它们都导入同一个 swift 文件并访问这些扩展名,即使我指定了,我也会收到Ambiguous use of someProperty/someClassMethod()"错误SwiftKit.UIView.someClassMethod() 形式的调用:

Then I created a project introduced these Frameworks, things is, if I import both of them in the same swift file and access those extensions, I got a "Ambiguous use of someProperty/someClassMethod()" error, even if I specified the call in the form of SwiftKit.UIView.someClassMethod() :

import UIKit
import SwiftKit
import SwiftFoundation

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.view.someProperty              // error: Ambiguous use of 'somProperty'
        SwiftKit.UIView.someClassMethod()   // error: Ambiguous use of 'someClassMethod()'
    }
}

如果我只导入其中一个,模棱两可的错误就会消失,但会发生更奇怪的事情:

If I only import one of them, the ambiguous error goes away, but more strange thing happens:

import UIKit
import SwiftKit
//import SwiftFoundation

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.view.someProperty
        SwiftKit.UIView.someClassMethod()
    }
}

控制台打印出来:

来自 Swift 基金会的 someProperty

someProperty from Swift Foundation

来自 Swift Foundation 的 someClassMethod

someClassMethod from Swift Foundation

我的问题是:我怎样才能毫不含糊地调用这些扩展(类/实例方法、属性)?如果我不能,是否意味着我们应该像通常使用 Objective-C 一样为扩展名添加前缀?

My question is: How can I call these extensions(both class/instance method, properties) without ambiguous? If I cannot, does it mean we should add prefix to extension names as we usually do with Objective-C?

推荐答案

详情

  • Swift 3、Xcode 8.1
  • Swift 4、Xcode 9.1
  • Swift 5.1、Xcode 11.2.1
  • 框架 SwiftFoundation 和 SwiftKit 具有相同名称的属性和函数

    frameworks SwiftFoundation and SwiftKit has the same names of the properties and functions

    使用不同的属性和函数名称

    Use different names of the properties and functions

    // SwiftFoundation
    public extension UIView {
        public class func swiftFoundationSomeClassMethod() {
            print("someClassMethod from Swift Foundation")
        }
    
        public var swiftFoundationSomeProperty: Double {
            print("someProperty from Swift Foundation")
            return 0
        }
    }
    

    方式2

    对属性和函数进行分组

    Way2

    Group the properties and functions

    // SwiftKit
    public extension UIView {
        public class SwiftKit {
            public class func someClassMethod() {
                print("someClassMethod from Swift Kit")
            }
    
            public var someProperty: Double {
                print("someProperty from Swift Kit")
                return 0
            }
        }
    
        var SwiftKit: SwiftKit { return SwiftKit() }
    }
    

    结果

    import UIKit
    import SwiftKit
    import SwiftFoundation
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            _ = view.SwiftKit.someProperty
            UIView.SwiftKit.someClassMethod()
    
            _ = view.swiftFoundationSomeProperty
            UIView.swiftFoundationSomeClassMethod()
        }
    }
    

    项目

    SwiftFoundation.UIView.swiftFoundationSomeClassMethod()
    

    您使用命名空间的变体不正确,因为这两个框架的所有 UIView 扩展都包含在您的 UIView 类中.查看下图,您可以在 SwiftFoundation 中看到 SwiftKit 类和 swiftFoundationSomeClassMethod().这可能会使其他开发人员感到困惑.

    Your variant of using the namespaces is not correct because all UIView extensions from both frameworks are included in you UIView class. Look at image bellow, you can see SwiftKit class and swiftFoundationSomeClassMethod() inside SwiftFoundation. This can confuse other developers.

    这篇关于Swift 扩展:两个模块中的相同扩展功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆