2

I recently encountered this syntax:

UIView *view=({
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    view.backgroundColor = [UIColor greenColor];
    view;
});

This might well be a duplicate but I don't know what to search for.

  • What is this syntax known as?
  • Has it always been supported or is it a recent addition?
  • Is it considered good or bad practice?
  • Are there any gotchas?
Robert
  • 37,670
  • 37
  • 171
  • 213
  • This probably works as a function, returning the view after creating it and setting the background colour. Never seen something similar :) – Basheer_CAD Mar 26 '14 at 09:30
  • @Basheer_CAD - Interesting, but there is no `return` statement, so surely its not a function? – Robert Mar 26 '14 at 09:30
  • @MartinR - Ah cool thanks! So its part of C. Do you have any info on the best practices of this technique? – Robert Mar 26 '14 at 09:32
  • @Robert: As explained in the GCC docs, it allows you to introduce local variables just for the computation of an expression. I don't think that there are any "gotchas", and it is more a matter of personal preference. In this particular case I do not see any advantage and would not use it. – Martin R Mar 26 '14 at 09:44

0 Answers0