I have a method I that takes a variable number of
+(NSString *)keyPathFromSelectors:(SEL)firstArg, ...
NSMutableArray *keys = [NSMutableArray array];
for (SEL arg = firstArg; arg != nil; arg = va_arg(args, SEL))
return [keys componentsJoinedByString:@"."];
return [NSSet setWithObject:[self keyPathFromSelectors:@selector(person), @selector(name),nil]];
Selectors are do not carry the guarantee that the method actually exists on the object in question. You can get compiler warnings that a selector name doesn't exist anywhere, but you won't get assurance that it does exist on the object it will be used on. For example, this compiles:
[person performSelector: @selector(applicationDidBecomeActive:)]
applicationDidBecomeActive exists on your app delegate and not
However! This kind of thing is possible through compiler macros. Look at the libextobjc library which provides exactly the kind of selector/key path compile time checking you're looking for. This is what powers a lot of the keypath magic in ReactiveCocoa but it can also be used standalone.