Basic Objective-C syntax commonly used in init methods [duplicate]

耗尽温柔 提交于 2019-12-25 04:58:05

问题


Possible Duplicate:
Double parentheses in sample code

Often the init method in an Objective-C class will have the following line:

if ((self = [super init])) { ...

I'm just wondering if the extra parentheses are necessary. Is the following line equivalent?

if (self = [super init]) { ...

Edit: This is indeed a duplicate of Double parentheses in sample code


回答1:


They are not necessary, but using (( and )) say "yes, I mean an assignment, not a comparison."

In fact clang will warn you about using assignment in an if condition and suggest double parentheses for unambiguity.

In general I think this could be considered bad coding style, but an exception is made here since this is a special frequently used pattern in obj-c



来源:https://stackoverflow.com/questions/12644657/basic-objective-c-syntax-commonly-used-in-init-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!