KVC-compliance using -respondsToSelector:
KVC-compliance using -respondsToSelector:
- Subject: KVC-compliance using -respondsToSelector:
- From: Brent Gulanowski <email@hidden>
- Date: Sat, 17 Jul 2004 02:48:59 -0400
If I write a class that doesn't implement -<value> and -set<value> for
some value, but I override -respondsToSelector: and
-forwardInvocation: to lie and say I do, will that be KVC compliant?
Will it support bindings?
I'm wrapping another class which can have varying properties. For
example if I had a dictionary ivar and wanted to respond to accessors
that matched keys to the dictionary. (It's not actually a dictionary
I'm dealing with -- I believe those are KVC for properties/keys
already, aren't they?) So I thought at first to just provide accessors
for the most important ones, but if I do the following, should it
work? It's 2am and I can't test it right now...
-(NSMethodSignature *)methodSignatureForSelector:(SEL)theSel {
NSMethodSignature *sig;
NSString *selString = NSStringFromSelector( theSel );
/* only care about messages formatted the way I'll format them */
if( [selString length] > 3 &&
[[selString subStringWithRange:NSMakeRange(0,3)]
isEqualToString:@"set"]) {
selString = [selString substringFromIndex:3];
}
if (nil != [[self attributeDictionary] valueForKey:selString]) {
/* generate a method signature -- I'm not going to try to write
this code in this e-mail */
return sig;
}
return [super methodSignatureForSelector:theSel];
}
-(BOOL)respondsToSelector:(SEL)theSel {
NSString *selString = NSStringFromSelector( theSel );
/* only care about messages formatted the way I'll format them */
if( [selString length] > 3 &&
[[selString subStringWithRange:NSMakeRange(0,3)]
isEqualToString:@"set"]) {
selString = [selString substringFromIndex:3];
}
if (nil != [[self attributeDictionary] valueForKey:selString])
return YES:
return [super respondsToSelector:theSel];
}
-(void) forwardInvocation(NSInvocation *)invocation {
NSString *selString = NSStringFromSelector([invocation selector]);
if(nil != [[self attributeDictionary] valueForKey:selString] ) {
[invocation setTarget:[self attributeDictionary]];
[invocation setSelector:NSSelectorFromString(@"valueForKey:"];
[invocation setArgument:selString atIndex:2];
[invocation invoke];
}
else if( [selString length] > 3 &&
[[selString subStringWithRange:NSMakeRange(0,3)]
isEqualToString:@"set"]) {
[invocation setTarget:[self attributeDictionary]];
[invocation setSelector:NSSelectorFromString(@"setValue:forKey:"];
/* I believe argument 2 is already set to what I want */
[invocation setArgument:[selString substringFromIndex:3] atIndex:3];
[invocation invoke];
}
[self doesNotRecognizeSelector:[invocation selector]];
}
Will this cause havok if a key/value pair gets removed, say due to
caching? What other kinds of problems could happen and can they be
fixed? Is it more trouble than it's worth to do all this just to
utilize bindings? Is it technically interesting enough to discuss it
anyway?
Cheers,
--
--
Brent Gulanowski
http://www.boredastronaut.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.