xcode Swift 语言中的命名空间和模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24104643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Namespaces and modules in the Swift language
提问by rhummelmose
I've been experimenting with Swift on my way home from WWDC. One of the most compelling new features of Swift, in my opinion, was namespacing. I haven't managed to get it to work as I expected it should though. Please see the attached screenshot and let me know if you have an idea of what I'm doing wrong.
我在从 WWDC 回家的路上一直在试验 Swift。在我看来,Swift 最引人注目的新特性之一是命名空间。我还没有设法让它像我预期的那样工作。请查看随附的屏幕截图,如果您知道我做错了什么,请告诉我。
EDIT: I have of course tried to remove the import statement.
编辑:我当然尝试删除导入语句。
采纳答案by rhummelmose
Turns out that this is a known bug: https://devforums.apple.com/message/976286#976286
原来这是一个已知的错误:https: //devforums.apple.com/message/976286#976286
回答by wottpal
I am sorry, if I search for "namespace" or "namespacing" in the Swift-eBook there are no results. Maybe you can give me a link to an additional resource?
抱歉,如果我在 Swift-eBook 中搜索“命名空间”或“命名空间”,没有结果。也许你可以给我一个额外资源的链接?
I would solve your problem simply with a struct
and static
functions.
我会用一个struct
和static
函数来解决你的问题。
ClassA
A类
struct ClassA {
static func localize() {
println("Localized String")
}
}
ClassB
B级
You can drop the import and execute the ClassA-function as follows:
您可以删除导入并执行 ClassA 函数,如下所示:
ClassA.localize()
The second way
第二种方式
In your case you can also just make an extension of String like so:
在您的情况下,您也可以像这样对 String 进行扩展:
extension String {
func localize() -> String {
return self+"-localized"
}
}
println("Test".localize()) // prints "Test-localized"