Re: A Data Object in Cocoa
Re: A Data Object in Cocoa
- Subject: Re: A Data Object in Cocoa
- From: Andy Lee <email@hidden>
- Date: Sat, 10 Jan 2009 15:13:34 -0500
On Saturday, January 10, 2009, at 12:18PM, "Michael Ash" <email@hidden> wrote:
>Mechanisms like KVC will
>look for ivars both with and without the underscore.
Yup:
file://localhost/Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.CoreReference.docset/Contents/Resources/Documents/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/SearchImplementation.html
>A cow-orker just pointed out to me that Apple does not, in fact,
>recommend against an underscore prefix for instance variables. They do
>recommend against them for private methods, but that's a completely
>different question. It seems that the well-known recommendation
>against _ivars is actually a misconception!
I think it is indeed a misconception. There is a risk with underscored methods because you may unknowingly override an undocumented Apple method. There is no such risk with underscored ivars. As you pointed out, the compiler will prevent collision of ivar names, and I confirmed it will do this even if the ivar is declared @private in the superclass.
@interface Animal : NSObject
{
@private
id _soul;
}
@end
@implementation Animal
@end
@interface Human : Animal
{
@private
id _soul; // <------ error: duplicate member '_soul'
}
@end
@implementation Human
@end
Anyway, I think the fact that _ivars are explicitly handled by KVC should lay the misconception to rest.
--Andy
_______________________________________________
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