问题描述
我正在看新的Swift.我来自C,C ++,Objective-C ...我注意到在Swift中不可能(?)分开声明和函数的定义.结果是结构和班级声明非常长且肿,因此很难通过查看代码来对课程进行"快速图片".难道我做错了什么?除了试图使功能较小之外,还有什么方法可以克服这个问题吗?预先感谢
推荐答案
在Swift中,声明与实施没有分离.这与大多数其他现代语言一样,例如Python.如果您想快速了解课程,则应使用代码折叠.只需折叠所有方法和功能.并展开您要修改/处理它的方法.
其他推荐答案
只是一种疯狂的公共API和私人实施的方式:
疯狂 - 代表工厂方法create(),我不喜欢在不真正需要的情况下在Swift中看到.
// 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") } }
用法:
let a = AClass.create() a.sayHello()
为什么它可以工作: private修饰符使得仅在源文件中可见.
优点:您可以定义多个实现,因此需要其他createOther().
缺点:看起来像Java或C#,不是快速的方法:)
其他推荐答案
关于我们能做的最好的方法是将协议用作实施声明,并标记所有私人方法:
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
问题在于它没有作为标准成语提供,命名不幸,我刚刚将"接口"附加到了类名.
问题描述
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
推荐答案
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.
其他推荐答案
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 :)
其他推荐答案
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.