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.
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.
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};
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