Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Objective-C 2.0 style question.



We got into this whole debate in the cocoa dev list about prefixes. I was using them for a while until i started doing Objective C 2.0 work. Then voted against using prefixes in ivars like _, m_, etc. The reasoning for this was i would like the accessors to be used when setting things using interface builder to setup interface objects. This keeps the pattern of using the accessors consistent.

Scott

On Apr 22, 2008, at 1:39 AM, Niko Matsakis wrote:

Comming from a C++ part of me says to always use the acessors if defined (unless private and not exposed), also using the accessors allows us to make sure our bindings are updated if its a bound value, but i see a mix of code and it may depend on if the iVar is to be exposed or not.. The company i work for is now getting into iPhone and 10.5 development and i want to stress the proper design choices.

As with the others who replied, I agree. In fact, if an instance variable contains accessor methods, then I use a naming convention (in my case, an "a_" prefix, for "accessor") to indicate that fact, which allows me to easily spot those locations in my code where I accidentally refer directly to the instance variable. This is particularly important when you are using key-value observing, as assigning directly to an instance variable will not notify anyone observing it.


In your case, that would mean:

@interface foo : NSObject
{
@private
	NSString* a_myString;
}

@property(copy) myString;

-(void)displayString;

@end

@implementation foo
@synthesize myString = a_myString;
@end

I find that in a flexible language like Obj. C (and I am not complaining about that flexibility!), such informal conventions go a long way towards preventing silly errors that other languages might prevent with new keywords and the like.


Niko

_______________________________________________ Do not post admin requests to the list. They will be ignored. Objc-language mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/objc-language/email@hidden

This email sent to email@hidden
References: 
 >Objective-C 2.0 style question. (From: Scott Andrew <email@hidden>)
 >Re: Objective-C 2.0 style question. (From: Niko Matsakis <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.