简体   繁体   English

Objective-C语法问题

[英]Objective-C syntax questions

I've found this code on this blog http://themainthread.com/blog/2014/02/building-a-universal-app.html 我在此博客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) ? static void initWithSimpleView(SimpleView *self)是什么意思? Why are imageView and label initialized in some kind of block? 为什么在某种块中初始化imageViewlabel

This code declares a C function named initSimpleView . 这段代码声明了一个名为initSimpleView的C函数。 The name initSimpleView is only visible within the file, because it is declared static . 名称initSimpleView仅在文件中可见,因为它被声明为static

The ({ ... }) is a GNU C extension called a “statement expression”. ({ ... })是GNU C扩展,称为“语句表达式”。 You can find more details about this usage in this Q&A . 您可以在本问答中找到有关此用法的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM