以NSTimer为例。构造时,除第一个参数(方法名称)外的所有参数都被命名为:
NSTimer *timer =
[NSTimer
scheduledTimerWithTimeInterval: seconds
target: self
selector: @selector(timerTick:)
userInfo: nil
repeats: repeats
];
为什么第一个参数没有命名?IE
NSTimer *timer =
[NSTimer scheduledTimerWithTimeInterval
interval: seconds
target: self
selector: @selector(timerTick:)
userInfo: nil
repeats: repeats
];
这背后是否有一些语言设计决策?它提供一些我在这里看不到的灵活性吗?
正如Apple文档所说,说“ Objective-C方法声明将参数作为其名称的一部分包括在内”确实更准确。
整个过程确定了方法。这就是为什么您询问的方法始于:
scheduledTimerWithTimeInterval
...这样它就自然地读取,而第一个参数没有附加名称。
来源:
作为某种存在的答案,为什么问题的答案通常很简单,“因为那是它的设计方式”。必须假定的Objective-C的创建者认为,与其他方法相比,这是一种更易读的方法声明方法。就是这样。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句