Skip navigation.
 
mlRe: @property problem
FROM : Jens Alfke
DATE : Mon Feb 11 23:11:03 2008

FYI: The property name doesn't have to match the instance variable 
name, so it's OK to keep using prefixes on your ivar names. So you 
could declare the class as

@interface MyWindow : NSWindow {
   BOOL _capturing;            // or mCapturing or whatever
}
@property(readwrite) BOOL    capturing;
@end

and then in the implementation use
   @synthesize capturing=_capturing;

I strongly recommend prefixing all instance variables, to avoid 
confusion with locals. I've also found out the hard way that it's even 
more important to do this with properties.

Not prefixing property instance vars can lead to a type of mistake 
where, in the implementation of the same class, you accidentally write 
something like
   contents = [NSArray array];
when you meant to write
   self.contents = [NSArray array];
Do you see the problem? Assuming no GC, and the 'contents' property is 
marked 'retain' or 'copy', the first line will cause a crash sometime 
later on, because you directly assigned an autoreleased value to an 
instance variable, so sometime after this method leaves scope, that 
array is going to be dealloced and 'contents' will be a bad pointer. 
The second line invokes the property's setter method, which correctly 
retains or copies the array.

—Jens

PS: To forstall an FAQ: Yes, it is kosher to use "_" as your ivar 
prefix, even though the Foundation and AppKit classes do the same. 
Name collisions are not a problem. It is, however, recommended to not 
use "_" as a method name prefix, because you can collide with and 
accidentally override internal superclass methods._______________________________________________

Cocoa-dev mailing list (<email_removed>)

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:
http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>

This email sent to <email_removed>

Related mailsAuthorDate
ml@property problem Randall Meadows Feb 11, 22:14
mlRe: @property problem Melissa J. Turner Feb 11, 22:27
mlRe: @property problem Kyle Sluder Feb 11, 22:28
mlRe: @property problem Joshua Emmons Feb 11, 22:29
mlRe: @property problem Shawn Erickson Feb 11, 22:33
mlRe: @property problem j o a r Feb 11, 22:35
mlRe: @property problem Brian Christensen Feb 11, 22:38
mlRe: @property problem Randall Meadows Feb 11, 22:56
mlRe: @property problem Jens Alfke Feb 11, 23:11
mlRe: @property problem Kyle Sluder Feb 11, 23:32
mlRe: @property problem j o a r Feb 11, 23:53
mlRe: @property problem Adam P Jenkins Feb 12, 00:33
mlRe: @property problem Bill Bumgarner Feb 12, 00:46
mlRe: @property problem Wade Tregaskis Feb 12, 00:46
mlRe: @property problem Kyle Sluder Feb 12, 01:08
mlRe: @property problem Nick Zitzmann Feb 12, 01:58
mlRe: @property problem Jens Alfke Feb 12, 04:44
mlRe: @property problem Andrew Farmer Feb 12, 06:25
mlRe: @property problem Sean McBride Feb 12, 16:58
mlRe: @property problem j o a r Feb 12, 19:48
mlRe: @property problem Jens Alfke Feb 12, 20:14
mlRe: @property problem Wade Tregaskis Feb 12, 20:35
mlRe: @property problem William Squires Feb 16, 23:35
mlRe: @property problem Jean-Daniel Dupas Feb 17, 01:03
mlRe: @property problem William Squires Feb 17, 17:59
mlRe: @property problem glenn andreas Feb 17, 18:11
mlRe: @property problem Bill Bumgarner Feb 17, 18:11
mlRe: @property problem Jim Correia Feb 17, 18:13
mlRe: @property problem William Squires Feb 17, 18:47
mlRe: @property problem mmalc crawford Feb 17, 18:57
mlRe: @property problem Bill Bumgarner Feb 17, 19:03
mlRe: @property problem Jim Correia Feb 17, 19:04
mlRe: @property problem William Squires Feb 17, 19:52
mlRe: @property problem Jean-Daniel Dupas Feb 17, 20:18
mlRe: @property problem Sherm Pendley Feb 17, 21:03
mlRe: @property problem Bill Bumgarner Feb 17, 21:09
mlRe: @property problem Bill Bumgarner Feb 17, 21:17
mlRe: @property problem Jens Alfke Feb 18, 00:06