1

I've found this code on this blog http://themainthread.com/blog/2014/02/building-a-universal-app.html

static void initSimpleView(SimpleView *self) {
    // Configure default properties of your view and initialize any subviews
    self.backgroundColor = [UIColor clearColor];

    self.imageView = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:imageView];
        imageView;
    });

    self.label = ({
        UILabel *label = [[UILabel alloc] init];
        label.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:label];
        label;
    });
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        initSimpleView(self);
    }
    return self;
}

How does it work? What is the meaning of static void initWithSimpleView(SimpleView *self)? Why are imageView and label initialized in some kind of block?

David Berry
  • 40,337
  • 12
  • 82
  • 94
tailec
  • 611
  • 7
  • 20

1 Answers1

5

This code declares a C function named initSimpleView. The name initSimpleView is only visible within the file, because it is declared static.

The ({ ... }) is a GNU C extension called a “statement expression”. You can find more details about this usage in this Q&A.

Community
  • 1
  • 1
rob mayoff
  • 358,182
  • 62
  • 756
  • 811