Objective C 方法语法澄清

3

我刚接触Objective C,正在学习一份在网上找到的教程。教程开始讲述消息传递和参数分离,并给出了一个示例:

When there's more than one argument, they're declared within the method name after the colons. Arguments break the name apart in the declaration, just as in a message.

- (void)setWidth:(float)width: height:(float)height;

我认为在width后面不应该有冒号,但我可能错了。根据我的研究,我相信这是一个笔误,但由于我是新手,所以我想确认一下。

方法是否只有setWidth: height:?还是在(float)width之后还有另一个参数,除了 height:(float)height

3个回答

2

这是一个笔误。方法签名应该是:

- (void)setWidth:(float)width height:(float)height;

方法名是setWidth:height:,你可以像这样调用它:

[someObject setWidth:aFloat height:anotherFloat];


1

你说得对。中间的冒号似乎是一个打字错误。在冒号后面,应该有一个变量占位符。如果在冒号后面有一个空格(就像这种情况),那么这是一个打字错误。


0

没错,你说得对。那是一个打字错误。你可以这样调用该方法:

[obj setWidth:100.0f height:200.0f];

在文档中引用该方法或用于方法回调时,应将其标记为 setWidth:height:(注意末尾的冒号)。祝你在接下来的教程中好运。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接