Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGRectMake alternative

I have seen this alternative to using CGRectMake() in order to initialise a CGRect variable:

CGRect frame = (CGRect){0,0,10,10};

My question is, how does CGRect frame = (CGRect){0,0,10,10}; work? What's going on behind the scenes? It looks like a c-style array is being initialised ({x,y,w,h}) which is then being cast as a CGRect struct - is this correct? If so, how is it possible to cast a c style array as a struct?

N.B. I am not asking if it is appropriate to use the above alternative to CGRectMake(), I only wish to understand why/how it works.

like image 227
Barjavel Avatar asked Aug 07 '12 08:08

Barjavel


3 Answers

It's a so-called compound literal. You can read more about them in this article by Mike Ash: Friday Q&A 2011-02-18: Compound Literals.

like image 156
omz Avatar answered Nov 08 '22 18:11

omz


U can use like this:

CGRect rect = CGRectFromString(@"{{0, 0}, {612, 892}}"); // it contents { CGPoint origin;CGSize size;};
NSLog(@"rect : %@",NSStringFromCGRect(rect));
like image 29
Paresh Navadiya Avatar answered Nov 08 '22 18:11

Paresh Navadiya


Check this:

CGPoint origin = {10, 20};
CGSize size = {100, 200};
CGRect rect = {origin, size};
like image 33
ILYA2606 Avatar answered Nov 08 '22 19:11

ILYA2606