为什么在Swift 3中没有重命名viewWithTag和其他方法? [英] Why isn't viewWithTag and some other methods renamed in Swift 3?

查看:181
本文介绍了为什么在Swift 3中没有重命名viewWithTag和其他方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift 3中,许多方法都被重命名了.根据WWDC的会话之一,方法名称中的介词被移至参数名称:

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")

所以我认为viewWithTag(1)将重命名为view(withTag: 1),但不是!

So I thought viewWithTag(1) will be renamed to view(withTag: 1), but it isn't!

API准则中甚至提到:

There is even mentioned in the API guidelines:

尤其是当参数类型为NSObject,Any,AnyObject或基本类型(如Int或String)时,使用时的类型信息和上下文可能无法完全传达意图.在此示例中,声明可能很清楚,但使用地点不明确.

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也没有重命名!

I also found that SKNode.addChild is not renamed as well!

问题:

为什么这些方法没有重命名?他们忘了他们吗?还是API准则有例外情况?如果是,那是什么?

Why are these methods not renamed? They forgot about them? Or are there exception cases to the API guidelines? If yes, what are them?

推荐答案

我正在研究

  • viewWithTag
  • 修剪名称的第一步是:

    1. 从类型保留转换的开头修剪结果类型. 具体来说,什么时候

    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部分实际上是第一个要删除的部分.

    the view part is actually the first to be removed.

    在第3步中也删除了forTag,因此结果是一个空的选择器.

    The forTag is removed too in step 3, therefore the result is an empty selector.

    修剪限制 ...

    • 切勿将选择器片完全清空.

    因此不执行修剪.

    1. addChild

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

    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.

    ...如果要删除GestureRecognizer,仅保留add,我们最终得到了一种方法,该方法在概念上修改了gestureRecognizers属性,但使用了一个过于通用的名称:

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

    这篇关于为什么在Swift 3中没有重命名viewWithTag和其他方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆