It seems that -valueForKey:
and -valueForKeyPath:
work with arbitrary methods, not only with properties. This seems very convenient:
I first stumbled upon it in Interface Builder, and then made some experiments:
// Thing.h
#import <Foundation/Foundation.h>
@interface Thing : NSObject
- (BOOL) alwaysYES;
- (BOOL) alwaysNO;
@end
// Thing.m
#import "Thing.h"
@implementation Thing
- (BOOL) alwaysYES
{
return YES;
}
- (BOOL) alwaysNO
{
return NO;
}
@end
I can call these methods via -valueForKey:
and -valueForKeyPath:
despite the fact that they are normal methods and no properties:
Thing *aThing = [[Thing alloc] init];
id result;
result = [aThing valueForKey:@"alwaysYES"];
NSLog(@"result is: %@", result);
result = [aThing valueForKeyPath:@"alwaysNO"];
NSLog(@"result is: %@", result);
Compiles, runs and gives the correct results. Is this documented anywhere? Can I safely use it? How can i understand it?