双括号方法语法。
我对某种类型的方法语法有疑问。
例如,我知道这是做什么的..
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
[CCDirector共享Director]
正在获取director的单例实例。然后,您将使用parameter
调用something
方法。这将类似于这样做[CCDirector sharedDirector]
is getting the singleton instance of the director. Then you are calling thesomething
method withparameter
. It would be similar to doing this[CCDirector共享Director]
是CCDirector
上的一个方法,并返回一些对象。很像[NSString alloc]
。该行的其余部分调用返回的对象上的方法。[CCDirector sharedDirector]
is a method onCCDirector
, and returns some object. Much like[NSString alloc]
. The rest of the line calls a method on the object that's returned.正如 @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.
括号在任何地方的含义都相同。消息表达式
[object arg0:value arg1:value ...]
将消息@selector(arg0:arg1:...)
发送到object (它本身可以是一个消息表达式)。消息处理程序可以返回一个对象,因此消息表达式的值本身可以是另一个消息的接收者。嵌套这些表达式与嵌套函数调用相同:
CCDirector_sharedDirector()
实际上并不存在;它只是用于比较。请注意:
init
并不是发送到 NSString 实例(事实并非如此,因为[NSString alloc]
实际上返回一个 NSCFString),就是将init
发送到[NSString alloc]
的结果。如果您发现嵌套消息难以阅读,可以将它们分成多行或使用临时变量。
The brackets mean the same everywhere. The message expression
[object arg0:value arg1:value ...]
sends the message@selector(arg0:arg1:...)
toobject
(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:CCDirector_sharedDirector()
doesn't actually exist; it's just used for comparison.Note that in:
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 thatinit
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.