• 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 problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: @property problem


  • Subject: Re: @property problem
  • From: Jens Alfke <email@hidden>
  • Date: Mon, 11 Feb 2008 14:11:03 -0800

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@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


  • Follow-Ups:
    • Re: @property problem
      • From: "Sean McBride" <email@hidden>
References: 
 >@property problem (From: Randall Meadows <email@hidden>)
 >Re: @property problem (From: "Kyle Sluder" <email@hidden>)
 >Re: @property problem (From: Brian Christensen <email@hidden>)

  • Prev by Date: re: split up alloc and init?
  • Next by Date: Re: Here is code to Convert RGB <-> HSB
  • Previous by thread: Re: @property problem
  • Next by thread: Re: @property problem
  • Index(es):
    • Date
    • Thread