在 Objective-C 中,逗号用作语句分隔符时有什么作用? [英] In Objective-C, what does a comma do when used as a statement separator?

查看:46
本文介绍了在 Objective-C 中,逗号用作语句分隔符时有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看来自第三方的一些源代码,并且反复看到对我来说是新的语法.基本上,它们用逗号而不是分号分隔语句.它编译并工作,但我不明白它在做什么.好像是这样

I am looking though some source code from a third party and am repeatedly seeing a syntax that is new to me. Basically they are separating statements with commas instead of semicolons. It compiles and works, but I don't understand what it is doing. It looks like so

if(url)[url release], url = nil;

他们有时也会在没有 if 的情况下使用它

and they also use it without the if sometimes

[url release], url = nil;

这是怎么回事?

推荐答案

和 C 和 C++ 一样,逗号运算符先计算左边的东西,然后计算右边的东西;表达式的整体值是右侧的值.基本上,这让一个表达式可以做两件事(左边的那个可能是副作用,比如方法调用或赋值).是的,语法与函数调用和变量声明中使用的语法有些不明确.

As in C and C++, the comma operator computes the thing on its left side and then computes the thing on the right; the overall value of the expression is the value of the right side. Basically, this lets a single expression do two things (the one on the left side being presumably for side effects such as a method call or assignment). Yes, the syntax is somewhat ambiguous with that used in function calls and variable declarations.

在可能的情况下,我更喜欢使用包含多个语句的诚实块.更长,但最终更清洁.也更容易调试.

I prefer to use an honest block containing multiple statements where possible. Longer, but ultimately cleaner. Easier to debug too.

这篇关于在 Objective-C 中,逗号用作语句分隔符时有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆