Why do properties require explicit typing during compilation?
- by ctpenrose
Compilation using property syntax requires the type of the receiver to be known at compile time. I may not understand something, but this seems like a broken or incomplete compiler implementation considering that Objective-C is a dynamic language.
The property "comment" is defined with:
@property (nonatomic, retain) NSString *comment;
and synthesized with:
@synthesize comment;
"document" is an instance of one of several classes which conform to:
@protocol DocumentComment <NSObject>
@property (nonatomic, retain) NSString *comment;
@end
and is simply declared as:
id document;
When using the following property syntax:
stringObject = document.comment;
the following error is generated by gcc:
error: request for member 'comment' in something not a structure or union
However, the following equivalent receiver-method syntax, compiles without warning or error and works fine, as expected, at run-time:
stringObject = [document comment];
I don't understand why properties require the type of the receiver to be known at compile time. Is there something I am missing? I simply use the latter syntax to avoid the error in situations where the receiving object has a dynamic type. Properties seem half-baked.