I got a snippet today,
self.tableView = ({
CGRect frame = CGRectMake(0.f, 0.f, kBPNavigationBarClockInOutAssociatesTableViewWidth,
kBPNavigationBarClockInOutAssociatesTableViewHeight);
UITableView * tableView =
[[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
tableView.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin
|UIViewAutoresizingFlexibleBottomMargin
|UIViewAutoresizingFlexibleWidth);
tableView.delegate = self;
tableView.dataSource = self;
[tableView setOpaque:NO];
[tableView setScrollEnabled:YES];
[tableView setBounces:YES];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[tableView setBackgroundView:nil];
[tableView setBackgroundColor:[UIColor clearColor]];
tableView;
});
- 1st question:
The ({}) is interesting that it can run the wrapped codes in sync mode and return the object of tableView at the end of its content.
I just suppose it is a block here, however why it is not look like:
UITableView * (^testBlock) (void) = ^{ // Codes here... return tableView;};
UITableView *myTableView = testBlock();
- 2nd question
The ({}) is not in async mode when I testing the code in Xcode, but in my experience, the block always (? also super junior question here: is block always running in async mode ?) runs in async mode. So why it is not here?
- 3rd question
When I google the block syntax with return object, I find that all the block returns primitive value such as BOOL and Double, and not lucky enough to find one that return an object. Is this true that a block can only return a primitive value?
reviese:
I find this snippet in Why doesn't this rudimentary Objective C-block code work?:
NSString *(^print_block)() = ^{
return @"this block worked!";
};
NSLog(@"%@", print_block());
So I think a block can also return a object type.
Many thanks!