在尝试使用 Swift 时,我发现了很多有用的方法和函数,它们都带有下划线前缀。例如,字符串有一个隐藏的 _split()
方法。由于某些原因,函数 _sin()
和 _cos()
(但不是 _tan
或 _sqrt
)也可用默认情况下。事实上,REPL 实际上建议我在键入 sin(2.0)
时使用这些函数。我没有导入 Foundation
或任何导入 Foundation
的内容。
为什么 Swift 提供这些“隐藏”函数,尤其是三角函数,我原以为它们是数学模块的一部分而不是内置函数。
最佳答案
Things beginning with underscores are not for public consumption and only exposed publicly for internal reasons (e.g. for testing because the standard library can't use @testable).
关于swift - Swift 下划线方法和函数有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40685231/