Separation of function declaration and definition in Swift

后端 未结 5 2066
遥遥无期
遥遥无期 2021-01-06 06:00

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

5条回答
  •  时光取名叫无心
    2021-01-06 06:19

    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.

提交回复
热议问题