Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS different kind of object initialisation [duplicate]

I was trying to integrate RESideMenu to my application. I was trying to understand how it works to be able to customize a little bit more. Than, i've encountered this initialisation that i've never seen before. I couldn't seem to find any official docs or any questions on SO which explains it a little bit more. If it does, please point me in the right direction.

Here is the code i'm talking about

self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 54 * 5) / 2.0f, self.view.frame.size.width, 54 * 5) style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
        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;
    });

So, Is it a new language feature that allows us to initialise objects a different way. It's kind of like Object Initializer in C#.

It would be any difference, if i had initialized like following

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 54 * 5) / 2.0f, self.view.frame.size.width, 54 * 5) style:UITableViewStylePlain];
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
[self.tableView setOpaque:NO];
....
...

Last question, does this kind of initialisation apply to any UIKit class or any Foundation class ?

EDIT If i comment this last line of code in the initialisation which is tableView; It gives me compile error which is Implicit conversion of 'BOOL' (aka 'signed char') to 'UITableView *' is disallowed with ARC, why is this happening ? The last line is kind of return statement or what ?

Thanks.

like image 651
limon Avatar asked May 26 '14 15:05

limon


1 Answers

Just FTR, there is no difference if you use the conventional code.

(Much like with json, say, you can now very simply say dict[@"someKey"] directly - but there's no fundamental difference.)

Thanks for pointing this out !!

like image 64
Fattie Avatar answered Sep 24 '22 06:09

Fattie