Created
April 25, 2011 06:36
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run | |
[Switching to process 27454] | |
Running… | |
Hello, World! | |
2011-04-24 23:34:24.389 test[27454:a0f] foobar: 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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...)