ObjC 中括号和花括号括起来的几个语句的赋值 [英] Assignment of several statements enclosed in parentheses and curly braces in ObjC
问题描述
我正在仔细阅读第三方 RESideMenu
框架的代码,并注意到一些奇怪的语法,但似乎工作得很好.这是令人困惑的一点:
I was perusing the code of the third-party RESideMenu
framework, and noticed some strange syntax that seemed to work just fine. Here is the confusing bit:
self.tableView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
tableView.autoresizingMask = mask;
tableView.delegate = self;
tableView.dataSource = self;
tableView.opaque = NO;
tableView.backgroundColor = [UIColor clearColor];
tableView.backgroundView = nil;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.bounces = NO;
tableView.scrollsToTop = NO;
tableView;
});
这个语法是如何工作的?我怀疑它与 C 级块作用域有关,但我以前从未见过这种情况.我也认为这可能是 Objc-2.0 文字的一个新特性,但我认为这不是真的.
How does this syntax work? I suspect it has something to do with a C-level block scoping, but I have never seen this before. I also considered it may be a new feature with Objc-2.0 literals, but I don't think that is true.
所以我想我的问题是它是如何工作的/是什么使它起作用?
So I guess my question is how does this work/what makes this work?
推荐答案
如 NSHipster:
神奇的背后是一个 GCC C 扩展,如果用方括号和圆括号括起来,它会导致代码块返回一个值.
Behind the magic is a GCC C extension, which causes a code block to return a value if enclosed within brackets and parentheses.
这不仅将配置细节隔离到初始化中,而且额外的作用域允许在后续初始化中重用框架、按钮和视图等通用变量名称.不再需要 loginButtonFrame = .../signupButtonFrame = ...!
This not only segregates configuration details into initialization, but the additional scope allows generic variable names like frame, button, and view to be reused in subsequent initializations. No more loginButtonFrame = ... / signupButtonFrame = ...!
这篇关于ObjC 中括号和花括号括起来的几个语句的赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!