Why do properties require explicit typing during compilation?

Posted by ctpenrose on Stack Overflow See other posts from Stack Overflow or by ctpenrose
Published on 2011-01-13T07:38:00Z Indexed on 2011/01/13 7:53 UTC
Read the original article Hit count: 457

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.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about properties