Search code examples
iosobjective-cscopeblock

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


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.


Solution

  • 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.