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 |
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
honestly, this just seems like a bug to me. KVC shouldn't allow one to break the access rules of the language. If an instance variable is marked "@Private" then it should be private and KVC shouldn't do an end-run around it.
I guess a class can override + accessInstanceVariablesDirectly, but honestly, that should be the default for private and protected instance variables, seems to me.
adding that does work:
add:
->
But really it should check for the scope directive, from the Objective-C Programming Language document:
"@Private The instance variable is accessible only within the class that declares it."
The difference here, of course, is that this pertains to the compiler and KVC is operating at the Objective-C runtime level.