Objective-C 中一行中的多个逗号分隔调用
最近,我看到下面这句话:
[someObject release], someObject = nil;
为什么这有效?为什么以及在什么情况下可以在一行中出现多个用,
分隔的调用? (不是 ;
)
Lately, I saw the following line:
[someObject release], someObject = nil;
Why does this work? Why and under which circumstances can there be several calls separated by ,
in one row? (Not ;
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Objective C 是 C 的超集,
,
是 C 中的运算符。它计算链中的最后一个表达式,并创建一个 序列点。分号
;
不能在表达式中使用,因为它不是运算符。您可能已经在涉及
for
循环的更常见情况下看到了,
运算符的工作情况:Objective C is a superset of C, and
,
is an operator in C. It evaluates to the last expression in the chain, and creates a sequence point.Semicolon
;
cannot be used in an expression because it is not an operator.You may have seen the
,
operator at work in a more common situation that involvesfor
loops:逗号运算符计算第一个操作数并丢弃结果,然后计算第二个操作数并返回其值。在本例中,第一个没有返回值,第二个的返回值为
nil
。The comma operator evaluates the first operand and discards the result, then evaluates the second and returns its value. The first has no return value, and the second has a return value of
nil
in this case.