Why {} works for C Structs but not for properties

The other day, I came across something to set the value of C Structs using curly braces, and it works fine, but I wonder why I can not use it to set 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}; 

Compiler Error "Expected Expression"

I think I know why this does not work, but I would like a more detailed explanation.

+4
source share
2 answers

The first lines combine the declaration and initialization of the variables. The compiler displays the type of the correct expression from the type of the left expression and distributes the numbers in the structure slots as expected.

However, the last lines are not initializations. Now the compiler will do nothing. As a result, the compiler does not know how it should interpret the material on the right. What is its type, how to place these numbers in memory? The compiler will not accept CGrect or CGPoint, and you should help it with an explicit cast (CGRect) {0,5,50,50}, which gives it the missing information.

+7
source

This should work:

 self.frame = (CGRect){0, 5, 50, 50}; imageview.center = (CGPoint){100, 120}; 
+3
source

Source: https://habr.com/ru/post/1414522/


All Articles