双括号方法语法。

发布于 2024-12-23 08:50:33 字数 505 浏览 1 评论 0原文

我对某种类型的方法语法有疑问。

例如,我知道这是做什么的..

NSString *theString = [[NSString alloc]init...blablabla];

分配是在类上执行的(在本例中为 NSString),而 init 是在类的实例上执行的。

如果我们知道

[variable method];

,我也知道它是如何起作用的.. 该方法被执行在“变量”上,

如果我有

variable = [instance method];

“方法”方法,则在实例上执行并存储在变量中。

但是我迷失的是看起来像这样的东西。

[[CCDirector sharedDirector]something:parameter];

我不确定在哪里执行什么操作。

I have a question about a certain type of method syntax.

For example I know what this does ..

NSString *theString = [[NSString alloc]init...blablabla];

The alloc is performed on the class (NSString in this case) and the init is performed on the instance of the class..

If we have

[variable method];

I know how that functions too.. the method is performed on "variable"

if I have

variable = [instance method];

the "method" method is performed on instance and stored in variable..

But where I get lost is at something that looks like this ..

[[CCDirector sharedDirector]something:parameter];

I'm not sure what action is being performed where..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

命硬 2024-12-30 08:50:33

[CCDirector共享Director]正在获取director的单例实例。然后,您将使用 parameter 调用 something 方法。这将类似于这样做

CCDirector* director = [CCDirector sharedDirector];
[director something:parameter];

[CCDirector sharedDirector] is getting the singleton instance of the director. Then you are calling the something method with parameter. It would be similar to doing this

CCDirector* director = [CCDirector sharedDirector];
[director something:parameter];
娇女薄笑 2024-12-30 08:50:33

[CCDirector共享Director]CCDirector上的一个方法,并返回一些对象。很像[NSString alloc]。该行的其余部分调用返回的对象上的方法。

[CCDirector sharedDirector] is a method on CCDirector, and returns some object. Much like [NSString alloc]. The rest of the line calls a method on the object that's returned.

一页 2024-12-30 08:50:33

正如 @hspain 指出的,它是用于表示单例实例的约定。实际的 alloc/init 发生,并且仅在sharedDirector 内发生一次。

这里是有关 Objective-C 中单例模式的更多信息

As @hspain pointed out, its a convention used to denote a singleton instance. The actual alloc/init occurs, and is only occured once, within sharedDirector.

Here is more information on the singleton pattern in objective-c.

穿透光 2024-12-30 08:50:33

括号在任何地方的含义都相同。消息表达式 [object arg0:value arg1:value ...] 将消息 @selector(arg0:arg1:...) 发送到 object (它本身可以是一个消息表达式)。消息处理程序可以返回一个对象,因此消息表达式的值本身可以是另一个消息的接收者。嵌套这些表达式与嵌套函数调用相同:

something(CCDirector_sharedDirector(), parameter);

CCDirector_sharedDirector() 实际上并不存在;它只是用于比较。

请注意:

[[NSString alloc] init...]

init 并不是发送到 NSString 实例(事实并非如此,因为 [NSString alloc] 实际上返回一个 NSCFString),就是将init发送到[NSString alloc]的结果。

如果您发现嵌套消息难以阅读,可以将它们分成多行或使用临时变量

[[CCDirector sharedDirector]
    something:parameter];

The brackets mean the same everywhere. The message expression [object arg0:value arg1:value ...] sends the message @selector(arg0:arg1:...) to object (which could itself be a message expression). The message handler can return an object, so the value of a message expression can itself be the recipient of another message. Nesting these expressions is the same as nesting function calls:

something(CCDirector_sharedDirector(), parameter);

CCDirector_sharedDirector() doesn't actually exist; it's just used for comparison.

Note that in:

[[NSString alloc] init...]

It's not so much that init is sent to an NSString instance (it isn't, as [NSString alloc] actually returns an NSCFString), it's that init is sent to the result of [NSString alloc].

If you find nested messages hard to read, you can break them into multiple lines or use temporary variables.

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