<tfoot id='IXziC'></tfoot>
  • <small id='IXziC'></small><noframes id='IXziC'>

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

          <bdo id='IXziC'></bdo><ul id='IXziC'></ul>
      1. <legend id='IXziC'><style id='IXziC'><dir id='IXziC'><q id='IXziC'></q></dir></style></legend>
      2. Swift 的标准库和名称冲突

        时间:2023-09-01

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

                  本文介绍了Swift 的标准库和名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我知道 Swift 不使用命名空间,但名称是在每个模块中定义的.首先,我不太明白这是如何避免名称冲突的 - 请随意详细说明.

                  I know that Swift doesn't use namespaces, but that names are defined within each module. First of all, I don't understand very well how this avoids name collisions -feel free to elaborate.

                  不过,我的主要问题是:假设我想要一个不使用 NSTreeNode 的树结构,所以我创建了自己的名为TreeNode"的类.现在假设 Apple 决定在 Swift 的标准库中包含一个用于构建树的类,并且正如预期的那样,他们将其命名为TreeNode".那会发生什么?我的自定义 TreeNode 将与标准库的 TreeNode 发生冲突...在这种情况下我是否必须更改所有代码?或者,Apple 会承诺 Swift 的标准库在未来不会改变?

                  Nevertheless, my main question is: Let's say I want a tree structure without using NSTreeNode, so I make my own class named "TreeNode". Now let's say that Apple decides to include a class to build trees in Swift's standard library, and, as expected, they name it "TreeNode". What happens then? My custom TreeNode will collide with Standard Library's TreeNode... Will I have to change all my code in cases like that? Or maybe Apple will promise that Swift's Standard Library will not change in the future?

                  这里回答了问题(感谢@Martin R 的评论)

                  Question answered here (thanks @Martin R for your comment)

                  推荐答案

                  Swift 中的命名空间是隐式的.所有类和其他符号都属于定义它们的目标(模块).因此,如果您定义类 String,则完全限定名称将是 MyTarget.String.当发生名称冲突时,您必须在类名前加上定义它的模块(框架),除非在当前模块中定义了具有该名称的类 - 此类优先且不需要前缀.

                  Namespacing in Swift is implicit. All classes and other symbols belong to the target (module) they are defined in. So if you define a class String the fully qualified name would be MyTarget.String. When there is a name collision, you have to prefix the class name with the module (framework) it is defined in, except when there is a class with that name defined in the current module - this class takes precedence and does not need to be prefixed.

                  struct String {
                      var swiftString = ""
                  }
                  
                  var a = String()
                  var b = Swift.String()
                  

                  因此,如果您创建了您的类 TreeNode 并且 Apple 后来也添加了一个 TreeNode,如果您只使用一个模块并且您不会需要改变任何东西.如果您想使用 Swift 的 TreeNode,则需要将其称为 Swift.TreeNode.

                  So if you create your class TreeNode and Apple later adds a TreeNode as well, your name would take precedence if you are using only one module and you wouldn't need to change anything. If you would want to use Swift's TreeNode, you would need to refer to it as Swift.TreeNode.

                  这篇关于Swift 的标准库和名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 Android 应用程序连接到 BlueMix 存储的 DB2 数据库 下一篇:Swift 扩展:两个模块中的相同扩展功能

                  相关文章

                  最新文章

                    <legend id='Hle9q'><style id='Hle9q'><dir id='Hle9q'><q id='Hle9q'></q></dir></style></legend><tfoot id='Hle9q'></tfoot>

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

                    • <bdo id='Hle9q'></bdo><ul id='Hle9q'></ul>

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