kam*_*soc 5 coding-style ios swift swift3
Swift 3中的函数命名约定让我有些困惑
我仔细研究了Swift 3 Guidelines,发现方法命名约定应如下所示:
func move(from start: Point, to end: Point)
x.move(from: x, to: y)
Run Code Online (Sandbox Code Playgroud)
如果我看一下我找到的UINavigationController方法pushViewController和presentViewController方法。方法调用如下所示:
navigationController?.pushViewController(viewController, animated: true)
navigationController?.present(controller, animated: true)
Run Code Online (Sandbox Code Playgroud)
在这里,我想知道为什么pushViewController方法调用不像Swift3那样。以及为什么这两种方法之间存在不一致。根据指导,我认为该push方法应如下所示:
rootNavigationController?.push(viewController, animated: true)
Run Code Online (Sandbox Code Playgroud)
那就更像Swift 3了。
//1
func saveName(_ name : String) {}
saveName("John")
//2
func save(_ name: String){}
save("John")
//3
func save(name: String){}
save(name: "John")
Run Code Online (Sandbox Code Playgroud)
在我看来,我认为选项3最适合Swift 3指南。但是另一方面,由于我的示例使用pushViewController和present(controller)方法,所以选项1也很好。
哪一个最适合《 Swift 3指南》的最佳选择?
由于@Sweeper的回答,它解决了为什么push和present方法之间存在不一致的问题。
https://github.com/raywenderlich/swift-style-guide
https://swift.org/documentation/api-design-guidelines/#parameter-names
请看这里:https : //github.com/apple/swift-evolution/blob/master/proposals/0005-objective-c-name-translation.md
它说:
- 永远不要从与封闭类的属性匹配的方法的基本名称中修剪后缀:
这种启发式方法的作用是防止我们为在概念上修改类属性的方法产生过于通用的名称。
...如果我们要删除 GestureRecognizer,只留下添加,我们最终会得到一个方法,该方法在概念上修改了gestureRecognizers 属性,但使用了一个过于通用的名称来这样做:
这就是pushViewController没有重命名的原因。在 中UINavigationController,有一个名为 的属性viewControllers。避免“过于通用的名称”。
当时为什么present改名?
请注意,present在UIViewController. UIViewController没有名为viewControlleror的属性viewControllers,因此该ViewController部分被修剪。
| 归档时间: |
|
| 查看次数: |
3092 次 |
| 最近记录: |