On 17.12.2012, at 13:14, Roland King wrote:

> 
> On 17 Dec, 2012, at 7:20 PM, Andreas Grosam <[email protected]> wrote:
> 
>> 
>> On 17.12.2012, at 11:25, Andreas Grosam wrote:
>> 
>>> I would like to use the property access syntax for a (readonly) property 
>>> which is defined in a category and sent to an object whose compile type 
>>> info is just "id". Use case:
>>> 
>>> id obj = …;
>>> if (obj.isFoo) {   // <== error: property 'isFoo' not found for object of 
>>> type '__strong id'.
>>>   ...
>>> }
>>> 
>>> 
>>> So, why does message send syntax compiles fine - but property syntax not??
>> 
>> Well, after thinking a while about the issue, I can answer this myself:
>> 
>> The compiler requires the type information in order to figure the actual 
>> getter signature. When declaring the a property, the getter and setter may 
>> be explicitly specified, for example:
>> 
>> @property (readonly, getter=getFooStateOrWhatever) isFoo;
>> 
>> Thus, the compiler cannot deduce the setter and getter methods from the 
>> property name unless it knows the type. So, in the case above where the type 
>> is not known at compile time, I cannot use dot syntax but needs to use 
>> method send syntax with the actual getter signature.
>> 
>> Andreas
>> 
> 
> Don't see that at all, sorry. The dot syntax a.foo is just another way of 
> saying [ a foo ], they are equivalent.
No, this is not the case. The compiler requires the declaration of the property 
when it encounters a dot syntax, in order to figure the *actual* method 
signature of the getter or setter. Most often, the method names for the setter 
and getter are the "canonical names" (`foo`, `setFoo:`), since per default the 
setter and getter are not specified. 

For example, the property could have been declared as

@property (readonly, getter=foo) BOOL isFoo;

So, in this case, in order to compile properly, the compiler will issue code 
for 

if (obj.isFoo) {
}

that is equivalent to

if ([obj foo]) {
}

and NOT 

if ([obj isFoo]) {
}


When the compiler does not, or cannot find the declaration of the property, it 
cannot just guess the name of the setter/getter. So, it signals an error. It 
cannot find the declaration of the property if the type is just `id`, for 
example. So, this will issue an error:

id obj = …
if (obj.isFoo) {
}

Whether it should signal an error or just issue a warning is debatable, IMO.



But, we are allowed to send any message to any object - using method send 
syntax:

id obj = …
if ([obj isFoo]) {   <== ohps!
}

id obj = …
if ([obj foo]) {   <== fine!
}




Andreas
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to