如何理解 Objective C 中多参数消息的语法?
我知道 Objective C 使用“交错参数”,这是设计使然。
但我想知道为什么您认为将第一个参数的名称合并到消息名称中会让生活更轻松。参见下文:
正确:[myRectangle setOriginX: 30.0 y: 50.0]
而不是
错误:[myRectangle setOrigin x: 30.0 y: 50.0]
[接收方消息 argument1:value1 argument2:value2...]
<<<这对你们来说是不是更加清晰直观呢?
I know Objective C uses 'interleaved arguments', and it is by design.
But I want to know why you think it makes life easier to merge the name of the first argument into the message name. See below:
Correct: [myRectangle setOriginX: 30.0 y: 50.0]
instead of
Wrong: [myRectangle setOrigin x: 30.0 y: 50.0]
[receiver message argument1:value1 argument2:value2...]
<<< isn't this one more clear and intuitive to you guys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试最短的答案:
是的:)
Trying for shortest answer:
Yes :)
它更容易实现,因为选择器实际上只是方法的“名称”作为字符串,并且参数可以按照给定的顺序传递给方法。这使得 Objective-C 可以很容易地编写为 C 之上的一个小型预处理器 + 一组运行时函数(它最初就是这样的)。否则的话会更加复杂。
它也更简单,因为 Objective-C 的消息传递语法源自 Smalltalk,它使用完全相同的方式来执行选择器(尽管它不是 C 的预处理器),因此与 Smalltalk 语法的变化为零。
您似乎在问为什么 Objective-C 没有从几十年后流行的语言中获取设计线索。答案是:因为他们还没有出现。 (我不确定当时关键字参数在 Lisp 中是否常见,但在大多数编程语言中并不常见。)
It's easier to implement because then selectors really are just the method's "name" as a string and the arguments can just be passed to the method in the order they were given. This allows Objective-C to be written easily as essentially a small preprocessor + set of runtime functions on top of C, which it originally was. To do it otherwise would be more complex.
It's also simpler because Objective-C's messaging syntax was derived from Smalltalk, which used the exact same way of doing selectors (though it was not a preprocessor to C), so this is zero change from the Smalltalk syntax.
You seem to be asking why Objective-C didn't take its design cues from languages that came into vogue many decades later. The answer would be: Because they weren't around yet. (I'm not sure if keyword arguments were common in Lisp by that time, but they weren't in most programming languages.)
我认为您正在尝试理解 Objective-C,将其与 Java 或 C++ 等 OOP 编程语言联系起来,我不推荐这样做。随着时间的推移你会理解并喜欢 Objc 方法,我花了 6 个月才真正享受它,现在我只喜欢 ObjC 开发:p。
无论如何,ObjC 人。说如下:
来源:方法和消息传递< /a>
编辑
ObjC 命名其方法的方式,我更喜欢它,因为我不必考虑方法名称,而是考虑我想发送给对象 A 这样我就能从中得到我想要的东西;
或者
尝试使用自然语言并将消息翻译为 object_a,例如:
“嘿,对象给我来自文学类别的所有作者都以“em”开头,并且来自 16 世纪” - 这是消息 - 你不需要单独的方法名称 - 所有这些都可以被视为方法名称(正如该男子所说)。
你现在明白 ObjC 做你以前所说的“方法重载”的方式是多么优雅和清晰了吗?
I think that you are trying to understand objective-c associating it with OOP programming languages like Java or C++, which I don't recommend. With time passing you will understand and like the Objc approach, it took me 6 months to really enjoy it, and now I only enjoy ObjC development :p.
Anyway the ObjC man. says the following:
Source: Methods and Messaging
Edit
The ObjC way of naming its methods, I like it better because I don't have to think about a method name, but about what messages ("represented by other objects") I want to send to object A so that I get what I want from it;
or
Try to use natural language and translate the message to object_a, something like:
"Hey object a give me from category Literature all the authors stargin with "em" and which are from cetury 16" - This is the Message - you don't need a separate method Name - all together can be seen as a method name (as the man says).
Do you get now how elegant and Clear is ObjC way to do what you used to call "method overloading"?