• 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: private methods and variables
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: private methods and variables


  • Subject: Re: private methods and variables
  • From: Torsten Curdt <email@hidden>
  • Date: Wed, 30 Jul 2008 04:45:27 +0200

Thanks for that!

Really interesting - but also sounds really terrible :)

Makes you wonder why stuff like hasn't been fixed in Objective-C 2.0.

cheers
--
Torsten

On Jul 30, 2008, at 04:03, Ken Thomases wrote:

On Jul 29, 2008, at 8:22 PM, Torsten Curdt wrote:

So how can I have private ivars that don't show up in the interface?

You can use the pImpl (pointer-to-implementation) idiom for classes that you're writing yourself:


@class MyPrivateStuff;
@interface MyClass : NSObject
{
	MyPrivateStuff* pImpl;
}

/* ... */

@end


Then, in your .m file:

@interface MyPrivateStuff : NSObject
{
	id foo;
	NSString* bar;
	/* ... */
};
@end

@implementation MyPrivateStuff
/* ... */
@end

@implementation MyClass

- (id) init
{
	if (self = [super init])
	{
		pImpl = [[MyPrivateStuff alloc] init];
		/* ... */
	}
}

@end



For classes which you are not writing yourself, you can still extend them with categories. For properties, you add the usual accessor methods, which is straightforward with a category. Providing per- instance backing storage for the properties is more complicated. One technique is to use a "category variable" (like the "class variable" we were discussing earlier; really just a static file- scope variable) to keep an associative collection which maps from "self" to a mutable dictionary of property values. NSMapTable is a good candidate for this sort of collection.

Cheers,
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


  • Follow-Ups:
    • Re: private methods and variables
      • From: Bill Bumgarner <email@hidden>
References: 
 >private methods and variables (From: Torsten Curdt <email@hidden>)
 >Re: private methods and variables (From: Ken Thomases <email@hidden>)
 >Re: private methods and variables (From: Torsten Curdt <email@hidden>)
 >Re: private methods and variables (From: "Kyle Sluder" <email@hidden>)
 >Re: private methods and variables (From: Ken Thomases <email@hidden>)
 >Re: private methods and variables (From: Torsten Curdt <email@hidden>)
 >Re: private methods and variables (From: Torsten Curdt <email@hidden>)
 >Re: private methods and variables (From: Ken Thomases <email@hidden>)

  • Prev by Date: Re: method naming
  • Next by Date: Re: method naming
  • Previous by thread: Re: private methods and variables
  • Next by thread: Re: private methods and variables
  • Index(es):
    • Date
    • Thread