问题描述
在Swift 3中,许多方法被更名.根据WWDC上的一个会话,方法名称中的介词移至参数名称:
UIView.animateWithDuration(1) -> UIView.animate(withDuration: 1) UIStoryboard.instantiateViewControllerWithIdentifier("some stuff") -> UIStoryboard.instantiateViewController(withIdentifier: "some stuff")
所以我认为viewWithTag(1)将被重命名为view(withTag: 1),,但不是!
API指南中甚至提到了:
尤其是当参数类型为nsobject时,任何int或string类型的int或string类型时,在使用点上的类型信息和上下文可能无法完全传达意图.在此示例中,声明可能很清楚,但是使用站点含糊不清.
func add(_ observer: NSObject, for keyPath: String) grid.add(self, for: graphics) // vague恢复清晰度,在每个弱键入参数之前,都有一个名词描述其作用:
func addObserver(_ observer: NSObject, forKeyPath path: String) grid.addObserver(self, forKeyPath: graphics) // clear
我还发现SKNode.addChild也没有重命名!
问题:
为什么这些方法未重命名?他们忘记了他们吗?还是API指南有例外情况?如果是,它们是什么?
推荐答案
我正在研究 Swift Evolution 0005 .
- ViewWithTag
名称修剪的第一步是:
修剪类型的结果类型. 具体而言,当
- 接收器类型与结果类型相同
- 和类型名称是在第一个选择器部分的头部匹配的
- 和匹配之后是介词
view零件实际上是第一个被删除的部分.
在步骤3中也删除了forTag,因此结果是一个空选择器.
与
发生碰撞修剪限制 ...
- 切勿将选择器完全为空.
因此,没有进行修剪.
- addchild
addChild,addSubview,addGestureRecognizer遵循所有相同的模式,实际上在规格中有一个示例:
- 切勿从与封闭类的属性相匹配的方法的基本名称中修剪后缀:
这种启发式的效果是防止我们生成过多的名称,以便从概念上修改类的属性.
...如果我们要删除GestureRecognizer,只剩下add,我们最终以一种概念上修改gestureRecognizers属性但使用过于通用的名称来做到这一点的方法:
通常,他们不能忘记某些方法,因为重命名(导入)是自动的.
问题描述
In Swift 3, a lot of the methods got renamed. According to one of the sessions at WWDC, the prepositions in method names are moved to the parameter name:
UIView.animateWithDuration(1) -> UIView.animate(withDuration: 1) UIStoryboard.instantiateViewControllerWithIdentifier("some stuff") -> UIStoryboard.instantiateViewController(withIdentifier: "some stuff")
So I thought viewWithTag(1) will be renamed to view(withTag: 1), but it isn't!
There is even mentioned in the API guidelines:
Especially when a parameter type is NSObject, Any, AnyObject, or a fundamental type such Int or String, type information and context at the point of use may not fully convey intent. In this example, the declaration may be clear, but the use site is vague.
func add(_ observer: NSObject, for keyPath: String) grid.add(self, for: graphics) // vagueTo restore clarity, precede each weakly typed parameter with a noun describing its role:
func addObserver(_ observer: NSObject, forKeyPath path: String) grid.addObserver(self, forKeyPath: graphics) // clear
I also found that SKNode.addChild is not renamed as well!
Question:
Why are these methods not renamed? They forgot about them? Or are there exception cases to the API guidelines? If yes, what are them?
推荐答案
I am looking into the algorithm described in Swift Evolution 0005.
- viewWithTag
The first step of name pruning is:
Prune the result type from the head of type-preserving transforms. Specifically, when
- the receiver type is the same as the result type
- and the type name is matched at the head of the first selector piece
- and the match is followed by a preposition
the view part is actually the first to be removed.
The forTag is removed too in step 3, therefore the result is an empty selector.
That collides with
Pruning Restrictions ...
- Never make a selector piece entirely empty.
Therefore no pruning is performed.
- addChild
addChild, addSubview, addGestureRecognizer follow all the same pattern, there is actually an example in the spec:
- Never prune a suffix from the base name of a method that matches a property of the enclosing class:
This heuristic has the effect of preventing us from producing too-generic names for methods that conceptually modify a property of the class.
... If we were to drop GestureRecognizer, leaving just add, we end up with a method that conceptually modifies the gestureRecognizers property but uses an overly generic name to do so:
In general, they cannot forget about some methods because the renaming (importing) is automatical.