Re: Using fake properties
Re: Using fake properties
- Subject: Re: Using fake properties
- From: Ken Thomases <email@hidden>
- Date: Sat, 17 Aug 2013 00:51:23 -0500
On Aug 17, 2013, at 12:31 AM, Gerriet M. Denkmann wrote:
> Currently I have stuff like:
>
> #if TARGET_OS_IPHONE
> NSLayoutManager *layoutManager = self.textView.layoutManager;
> #else // TARGET MAC OS X
> NSLayoutManager *layoutManager = [ self.textView layoutManager ];
> #endif // TARGET
>
> But I noticed that OS X also accepts:
> layoutManager = self.textView.layoutManager;
>
> 1. Is this a save idiom to use?
Yes.
> 2. If so, is it a good idea to treat method calls like properties?
It's a good idea to treat properties like properties. See below…
> 3. If so, why doesn't NSTextView not declare this method as a (readonly) property?
Properties existed well before _declared propreties_ (@property) were added to the language. The declaration of a getter method defines a readable property. The further declaration of a setter defines a read-write property. There's no need for an @property declaration. @property is a shortcut – it declares the accessor methods for you – plus some goodies: it gives some additional information about semantics like ownership policy. The Objective-C runtime is aware of declared properties (i.e. class_copyPropertyList()). In the implementation (which you shouldn't care about for classes other than your own), the compiler can use a declared property to synthesize instance variables and the accessors.
All of that is separate from dot syntax. Dot syntax is just syntactic sugar for accessing properties through the accessors. All properties, not just declared properties.
NSTextView does declare layoutManager as a property. It just does so in the old way, using a getter method, rather than a declared property, using @property. Either way, you can safely use dot syntax to access that property.
Regards,
Ken
_______________________________________________
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