iOS Name of this way of building and returning an object in Objective-C

北城余情 提交于 2019-12-17 14:56:08

问题


I'm trying to find out what this style of coding is called, is it an inline block? inline scope? what? What will the compiler create when it comes across one of these...

- (UIView *)createMyView {
      return
        ({
           UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 0)];
            /* set some stuff up on the view;
   */     
           view;

        });
}

I'm asking because we're getting a lot of cxx_destruct calls in a crash log with lines numbers that are way bigger than the actual size of the file. I'm wondering if this way of coding adds some weird stuff to the way its built.


回答1:


That is a "Statement Expression", which is a GCC feature (understood by Clang as well), see http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html:

A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression.

The value of the expression is the value of the last subexpression in the compound statement.



来源:https://stackoverflow.com/questions/21909511/ios-name-of-this-way-of-building-and-returning-an-object-in-objective-c

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