Skip to content

Instantly share code, notes, and snippets.

@woolsweater
Last active December 12, 2015 07:09
Show Gist options
  • Save woolsweater/4734460 to your computer and use it in GitHub Desktop.
Save woolsweater/4734460 to your computer and use it in GitHub Desktop.
Private ivar that is accessible in subclasses.
#import "Monongahela.h"
@interface FortHill : Monongahela
@end
#import "FortHill.h"
#import "Monongahela_Private.h"
// Only override getter; use superclass's setter
- (CGPoint)frog
{
return CGPointMake(frog.x + 2.0, frog.y + 5.5);
}
// Override both setter and getter
- (NSData *)crow
{
NSLog(@"This is my crow.");
return crow; // This is the ivar declared in the class extension
}
- (void)setCrow:(NSData *)newCrow
{
NSLog(@"What a lovely new crow!");
crow = [newCrow copy];
}
@end
#import <Foundation/Foundation.h>
@interface Monongahela : NSObject
{
CGPoint frog;
}
@property (assign, nonatomic) CGPoint frog;
@property (copy, nonatomic) NSData * crow;
@property (strong, nonatomic) NSPipe * aardvark;
@end
#import "Monongahela.h"
#import "Monongahela_Private.h"
@implementation Monongahela
@synthesize frog; // Use existing ivar from public header; create getter and setter
@synthesize crow; // Use existing ivar from extension; create getter and setter
@synthesize aardvark; // Create ivar; use getter and setter below
- (NSPipe *)aardvark
{
NSLog(@"Here's your aardvark!");
return aardvark;
}
- (void)setAardvark:(NSPipe *)newAardvark
{
NSLog(@"Saving a new aardvark.");
aardvark = newAardvark;
}
@end
#import "Monongahela.h"
@interface Monongahela ()
{
@protected // Ivars in class extensions are @private by default
NSData * crow;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment