Objective-C语法问题

时间:2015-01-23 16:51:18

标签: ios objective-c c static 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;
}

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

1 个答案:

答案 0 :(得分:5)

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

({ ... })是一个名为“语句表达式”的GNU C扩展。您可以找到有关此用法的更多详细信息in this Q&A