views:

46

answers:

1

Could someone explain why the second syntax of the same expression does not work? If you need some background, manyViews is a pointer to an NSMutableArray loaded with UIView objects.

[[manyViews objectAtIndex:0] setFrame:CGRectMake(30,30,100,20)];  // works as intended

[manyViews objectAtIndex:0].frame = CGRectMake(30,30,100,20);     // compiler does not recognize the "frame" member
+1  A: 

Current limitation of the compiler (both gcc and Clang). Dot notation is hopelessly overloaded, and the compiler thinks you're trying to assign a field in a struct. The id isn't a struct, so it gives an error. I use it myself, but I still say dot notation was definitely the worst addition in ObjC2. It creates a lot of confusion for programmers, and as we see here, often creates confusion for the compiler.

Later versions of gcc or clang may work out a way to parse this correctly.

Rob Napier
I'll continue to make a habit of using the message syntax for objects and try to leave dot notations for structs as much as possible. Thanks!
bitcruncher