Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

U can use like this:

CGRect rect = CGRectFromString(@"{{0, 0}, {612, 892}}"); // it contents { CGPoint origin;CGSize size;};
NSLog(@"rect : %@",NSStringFromCGRect(rect));

Check this:

CGPoint origin = {10, 20};
CGSize size = {100, 200};
CGRect rect = {origin, size};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top