Objective-C语法问题

tai*_*lec 1 c static objective-c ios objective-c-blocks

我在这个博客上发现了这个代码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;
}
Run Code Online (Sandbox Code Playgroud)

它是如何工作的?是什么意思static void initWithSimpleView(SimpleView *self)?为什么imageViewlabel在某种块的初始化?

rob*_*off 5

此代码声明了一个名为的C函数initSimpleView.该名称initSimpleView仅在文件中可见,因为它已声明static.

({ ... })是一个名为"语句表达式"的GNU C扩展.您可以在此问答中找到有关此用法的更多详细信息.