【问题标题】:Objective-C syntax questionsObjective-C 语法问题
【发布时间】:2015-01-23 16:51:17
【问题描述】:

我在这个博客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 在某种块中初始化?

【问题讨论】:

    标签: ios objective-c c static objective-c-blocks


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多