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

Yep. KVC has a lot of benefits when used judiciously, but it also has some serious trade-offs.

@haikusw
Copy link
Author

haikusw commented Apr 25, 2011

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:

@implementation Foo

+ (BOOL) accessInstanceVariablesDirectly
{
    return NO;
}

->

2011-04-24 23:46:42.455 test[27513:a0f] *** Terminating app due to uncaught exception 
'NSUnknownKeyException', reason: '[<Foo 0x10010c630> valueForUndefinedKey:]: 
this class is not key value coding-compliant for the key bar.'

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.

@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