• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Property Declared in a Category and Sent to id
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Property Declared in a Category and Sent to id


  • Subject: Re: Property Declared in a Category and Sent to id
  • From: Andreas Grosam <email@hidden>
  • Date: Mon, 17 Dec 2012 15:17:31 +0100

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

>
> On 17 Dec, 2012, at 7:20 PM, Andreas Grosam <email@hidden> 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@hidden)

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:

This email sent to email@hidden


References: 
 >Re: Property Declared in a Category and Sent to id (From: Andreas Grosam <email@hidden>)
 >Re: Property Declared in a Category and Sent to id (From: Roland King <email@hidden>)

  • Prev by Date: [SOLVED] Re: Dragging CALayer in an event loop
  • Next by Date: Re: NSWorkspace recycleURLs:completionHandler error -5000 (afpAccessDenied)
  • Previous by thread: Re: Property Declared in a Category and Sent to id
  • Next by thread: Simple question about storyboard
  • Index(es):
    • Date
    • Thread