Skip to content

Instantly share code, notes, and snippets.

@haikusw
Created April 25, 2011 06:36
Show Gist options
  • Save haikusw/940235 to your computer and use it in GitHub Desktop.
Save haikusw/940235 to your computer and use it in GitHub Desktop.
Why I don't like KVC - lets you access private instance variables; breaks OOP encapsulation.
#include <stdio.h>
#include <Foundation/Foundation.h>
// -----
@interface Foo : NSObject
{
@private
NSNumber* bar;
}
@end
@implementation Foo
- (id) init
{
self = [super init];
if ( self )
{
bar = [[NSNumber alloc] initWithInteger: 12];
}
return self;
}
@end
// --------
int main (int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Foo * foo = [[Foo alloc] init];
NSLog(@"foobar: %@", [foo valueForKey: @"bar"] );
[foo release];
foo = nil;
[pool release];
return 0;
}
run
[Switching to process 27454]
Running…
Hello, World!
2011-04-24 23:34:24.389 test[27454:a0f] foobar: 12
@jonsterling
Copy link

Well put. Maybe this will be improved in a future version of the runtime!

(I'd also love to see private instance variables leave public interfaces in Apple's frameworks one day; once everyone's switched to 64-bit...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment