6

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.

Declaring module global function Attempting to utilize module function EDIT: I have of course tried to remove the import statement.

rhummelmose
  • 647
  • 4
  • 15

2 Answers2

4

Turns out that this is a known bug: https://devforums.apple.com/message/976286#976286

rhummelmose
  • 647
  • 4
  • 15
0

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?

I would solve your problem simply with a struct and static functions.

ClassA

struct ClassA {
    static func localize() {
        println("Localized String")
    }
}

ClassB

You can drop the import and execute the ClassA-function as follows:

ClassA.localize()


The second way

In your case you can also just make an extension of String like so:

extension String {
    func localize() -> String {
        return self+"-localized"
    }
}

println("Test".localize()) // prints "Test-localized"
kellyfj
  • 6,586
  • 12
  • 45
  • 66
Dennis Zoma
  • 2,621
  • 2
  • 17
  • 27
  • The concept of namespacing does not have to be described with that name in order to be exactly that. What I try to do is access a function, global to another module and as such in another namespace, from my code. Using classes, structs or extensions to achieve the syntax doesn't solve the problem. Turns out that this is a known bug: https://devforums.apple.com/message/976286#976286 – rhummelmose Jun 08 '14 at 14:00
  • And on a side note. Namespaces were actually on the slides as they announced Swift to begin with. – rhummelmose Jun 08 '14 at 14:02