In Objective-C, what does a comma do when used as a statement separator?

前端 未结 3 1364
春和景丽
春和景丽 2021-02-12 16:22

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 semi

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-12 17:17

    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.

提交回复
热议问题