<tfoot id='WrXit'></tfoot>

  • <small id='WrXit'></small><noframes id='WrXit'>

        <bdo id='WrXit'></bdo><ul id='WrXit'></ul>

      1. <legend id='WrXit'><style id='WrXit'><dir id='WrXit'><q id='WrXit'></q></dir></style></legend>
        <i id='WrXit'><tr id='WrXit'><dt id='WrXit'><q id='WrXit'><span id='WrXit'><b id='WrXit'><form id='WrXit'><ins id='WrXit'></ins><ul id='WrXit'></ul><sub id='WrXit'></sub></form><legend id='WrXit'></legend><bdo id='WrXit'><pre id='WrXit'><center id='WrXit'></center></pre></bdo></b><th id='WrXit'></th></span></q></dt></tr></i><div id='WrXit'><tfoot id='WrXit'></tfoot><dl id='WrXit'><fieldset id='WrXit'></fieldset></dl></div>

        Swift 扩展:两个模块中的相同扩展功能

        时间:2023-09-01
          <bdo id='W2aYK'></bdo><ul id='W2aYK'></ul>

          <small id='W2aYK'></small><noframes id='W2aYK'>

          • <i id='W2aYK'><tr id='W2aYK'><dt id='W2aYK'><q id='W2aYK'><span id='W2aYK'><b id='W2aYK'><form id='W2aYK'><ins id='W2aYK'></ins><ul id='W2aYK'></ul><sub id='W2aYK'></sub></form><legend id='W2aYK'></legend><bdo id='W2aYK'><pre id='W2aYK'><center id='W2aYK'></center></pre></bdo></b><th id='W2aYK'></th></span></q></dt></tr></i><div id='W2aYK'><tfoot id='W2aYK'></tfoot><dl id='W2aYK'><fieldset id='W2aYK'></fieldset></dl></div>
              <tbody id='W2aYK'></tbody>
            <tfoot id='W2aYK'></tfoot>

                  <legend id='W2aYK'><style id='W2aYK'><dir id='W2aYK'><q id='W2aYK'></q></dir></style></legend>

                  本文介绍了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 扩展:两个模块中的相同扩展功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Swift 的标准库和名称冲突 下一篇:命名空间冲突真的是 Objective-C 中的一个问题吗

                  相关文章

                  最新文章

                1. <i id='fTZPK'><tr id='fTZPK'><dt id='fTZPK'><q id='fTZPK'><span id='fTZPK'><b id='fTZPK'><form id='fTZPK'><ins id='fTZPK'></ins><ul id='fTZPK'></ul><sub id='fTZPK'></sub></form><legend id='fTZPK'></legend><bdo id='fTZPK'><pre id='fTZPK'><center id='fTZPK'></center></pre></bdo></b><th id='fTZPK'></th></span></q></dt></tr></i><div id='fTZPK'><tfoot id='fTZPK'></tfoot><dl id='fTZPK'><fieldset id='fTZPK'></fieldset></dl></div>

                  <legend id='fTZPK'><style id='fTZPK'><dir id='fTZPK'><q id='fTZPK'></q></dir></style></legend>

                    1. <tfoot id='fTZPK'></tfoot>
                        <bdo id='fTZPK'></bdo><ul id='fTZPK'></ul>

                      <small id='fTZPK'></small><noframes id='fTZPK'>