I came across something the other day for setting the value of C Structs by using curly braces and it works great but I'm wondering why exactly can I not use it for setting the value of properties?
//This works:
CGPoint pt = {10, 20};
CGRect rct = {0, 5, 50, 50};
//But this doesn't:
self.frame = {0, 5, 50, 50};
imageview.center = {100, 120};
The compiler error is "expected expression"
I think I know why this doesn't work, but I would like a more thorough explanation.
The first lines combine declaration and initialisation of variables. The compiler infers the type of the right expression from the type of the left expression, and distributes the numbers in the struct slots as expected.
The last lines, however, are not initialisations. Now the compiler won't infer anything. As a consequence, the compiler doesn't know how it should interpret the stuff on the right. What is its type, how to layout those numbers in memory ? The compiler won't assume its CGrect or CGPoint, and you must help him with an explicit cast (CGRect){0,5,50,50} which gives him the missing information.
This should work:
self.frame = (CGRect){0, 5, 50, 50};
imageview.center = (CGPoint){100, 120};
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