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;
});
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?
As mentioned on NSHipster:
Behind the magic is a GCC C extension, which causes a code block to return a value if enclosed within brackets and parentheses.
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 = ...!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With