如何理解 Objective C 中多参数消息的语法?

发布于 2024-09-14 19:51:46 字数 317 浏览 3 评论 0原文

我知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮生面具三千个 2024-09-21 19:51:46

尝试最短的答案:

是的:)

Trying for shortest answer:

Yes :)

尐籹人 2024-09-21 19:51:46

它更容易实现,因为选择器实际上只是方法的“名称”作为字符串,并且参数可以按照给定的顺序传递给方法。这使得 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.)

琉璃繁缕 2024-09-21 19:51:46

我认为您正在尝试理解 Objective-C,将其与 Java 或 C++ 等 OOP 编程语言联系起来,我不推荐这样做。随着时间的推移你会理解并喜欢 Objc 方法,我花了 6 个月才真正享受它,现在我只喜欢 ObjC 开发:p。

无论如何,ObjC 人。说如下:

方法的实际名称
(insertObject:atIndex:) 是一个
所有签名的串联
关键字,包括冒号字符。
冒号字符声明
参数的存在。如果有一个方法
没有参数,省略冒号
在第一个(也是唯一一个)签名之后
关键词。在这个例子中,该方法
采用两个参数。

来源:方法和消息传递< /a>

编辑
ObjC 命名其方法的方式,我更喜欢它,因为我不必考虑方法名称,而是考虑我想发送给对象 A 这样我就能从中得到我想要的东西;

NSArray *list = [object_a fromCategory:@"Literature" authorsStartingWith:@"Em"];

或者

NSArray *list2 = [object_a fromCategory:@"Literature" authorsStarginWith:@"EM" fromCentury:@"16"]

尝试使用自然语言并将消息翻译为 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:

The method’s actual name
(insertObject:atIndex:) is a
concatenation of all of the signature
keywords, including colon characters.
The colon characters declare the
presence of a parameter. If a method
has no parameters, you omit the colon
after the first (and only) signature
keyword. In this example, the method
takes two parameters.

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;

NSArray *list = [object_a fromCategory:@"Literature" authorsStartingWith:@"Em"];

or

NSArray *list2 = [object_a fromCategory:@"Literature" authorsStarginWith:@"EM" fromCentury:@"16"]

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文