Separation of function declaration and definition in Swift

断了今生、忘了曾经 提交于 2019-12-01 04:38:36

问题


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 functions. The result of this is that structure and class declarations are very long and bloated, and it is therefore difficult to have a "quick picture" of the class by looking at the code. Am I doing something wrong? Is there any approach to overcome this problem, besides trying to make functions small, etc.? Thanks in advance


回答1:


In swift, there is no separation of declaration vs implementation. This works like most other modern languages like Python. If you want to get a quick picture of your class, you should use code folding. Simply fold all your methods and functions. And unfold a method of you want to modify / work on it.




回答2:


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 :)




回答3:


About the best we can do is use protocols as Implementation declarations and mark all private methods:

protocol TestInterface {
    var propertyPublic1: String { get }
    func funcPublic1() -> Int
}

class Test : TestInterface {
    var propertyPublic1: String = "text."
    func funcPublic1() -> Int {return 754}

    private var propertyPrivate1: Int = 5678
    private func funcPrivate1() -> Int {return 334}
}

var t = Test()
let propPublic1 = t.propertyPublic1

The problem is that it is not provided as a standard idiom and the naming is unfortunate, I have just appended "Interface" to the class name.




回答4:


Swift doesn't let you separate declarations from implementations. This cuts both ways -- on the one hand, you can't (manually) write a header file that describes the interface you want to expose for users of your code. On the other, you don't have to manually keep your header files up to date and in sync with your implementations.

Instead, Swift has an access control mechanism. Use the public, internal (implied), and private keywords to mark which types and functions you intend to expose.




回答5:


Yes, it's true that swift doesn't have header files but you can make Xcode generate something similar to this.

This will give you an overview of the class as you mention.

On Xcode go to Navigation > Jump to Generated Interface.

This link should explain a bit more.



来源:https://stackoverflow.com/questions/25210547/separation-of-function-declaration-and-definition-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!