• 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: @properties and Information Hiding
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: @properties and Information Hiding


  • Subject: Re: @properties and Information Hiding
  • From: Chris Hanson <email@hidden>
  • Date: Thu, 1 Nov 2007 19:13:22 -0700

On Nov 1, 2007, at 6:07 AM, Karl Goiser wrote:

Along these lines, I am looking for advise on how to implement a pattern that I am sure many people use: having a private setter and a public getter. Before properties, I'd implement the two methods in the implementation part and only declare the getter in the interface part. But now, in the 2.0 world, how can I leverage the magic of synthesis to implement such a pattern?

This is what I do. I follow this general pattern in all of my coding, and it really helps keep "internals" isolated from the public interface the rest of the code should know about, while also helping out my unit tests (since they do test the internals).


Hope this helps!

  -- Chris


// Foo.h - public class declaration for Foo

#import <Foundation/Foundation.h>

@interface Foo : NSObject {
@private
  id _selectedObject;
}

// selectedObject changes based on some user action.
@property(readonly, retain) selectedObject;
@end

// Foo_Internal.h - internal class declaration for Foo
// Only used by Foo.m and the unit tests that specify Foo's behavior

#import "Foo.h"

// this is an Objective-C 2.0 "class extensions"
@interface Foo ()

// a property can be promoted from readonly to readwrite in a class extension
@property(readwrite, retain) selectedObject;
@end


// Foo.m - Foo's implementation

#import "Foo_Internal.h"

@implementation Foo

// generates both -selectedObject and -setSelectedObject: thanks to the second @property
@synthesize selectedObject = _selectedObject;


- (void)dealloc {
    self.selectedObject = nil;

    [super dealloc];
}

@end

_______________________________________________

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


References: 
 >@properties and Information Hiding (From: Karl Goiser <email@hidden>)

  • Prev by Date: Re: [NSOutlineView outlineView:isGroupItem] rendering issue
  • Next by Date: Re: CILinearGradient drawing backwards in 10.5
  • Previous by thread: Re: @properties and Information Hiding
  • Next by thread: Symbol not found: _NSDefaultRunLoopMode
  • Index(es):
    • Date
    • Thread