I\'m having a look at the new Swift. I come from C, C++, Objective-C... I notice that in swift is not possible (?) to separate the declaration and the definition of function
Just a crazy way to have public API and private implementation:
crazy - stands for factory method create() which I do not like to see in swift without real need.
In AClass.swift file:
// Public API
class AClass {
private init() {
}
class func create() -> AClass {
return AClassPrivateImplementation()
}
// Only method declaration
func sayHello() {}
}
// Private implementation
private class AClassPrivateImplementation: AClass {
override func sayHello() {
privateImplementation()
}
func privateImplementation() {
println("Hello")
}
}
Usage:
let a = AClass.create()
a.sayHello()
Why it works: private modifier makes things visible only within source file.
Advantages: you can define more than one implementation hence additional createOther() will be needed.
Disadvantages: it looks like Java or C#, not a Swift way :)