为什么 viewWithTag 和其他一些方法没有在 Swift 3 中重命名?

Why isn't viewWithTag and some other methods renamed in Swift 3?

在Swift3中,很多方法都被重命名了。根据 WWDC 的一个会议,方法名称中的介词被移动到参数名称中:

UIView.animateWithDuration(1)
 -> UIView.animate(withDuration: 1)

UIStoryboard.instantiateViewControllerWithIdentifier("some stuff")
 -> UIStoryboard.instantiateViewController(withIdentifier: "some stuff")

所以我以为 viewWithTag(1) 会重命名为 view(withTag: 1) 但事实并非如此!

甚至在API指南中提到:

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) // vague

To 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

我还发现SKNode.addChild也没有改名!

问题:

为什么不重命名这些方法?他们忘了他们?还是 API 准则存在例外情况?如果是,它们是什么?

我正在研究 Swift Evolution 0005 中描述的算法。

  1. viewWithTag

名称修剪的第一步是:

  1. 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

view 部分实际上是第一个被删除的部分。

forTag 在步骤 3 中也被删除,因此结果是一个空选择器。

相撞

Pruning Restrictions ...

  • Never make a selector piece entirely empty.

因此不进行修剪。

  1. 添加孩子

addChildaddSubviewaddGestureRecognizer 遵循相同的模式,规范中实际上有一个示例:

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

总的来说,他们不能忘记一些方法,因为重命名(导入)是自动的。