How can I invoke a method at runtime on an Objective-C class when all I have is it's signature in string form:
NSString* typeName = @"Widgets";
NSString* methodName = [NSString stringWithFormat:@"add%@Object:", typeName];
Note that the method name can change at runtime but the number of arguments stays fixed - one in this instance.
You really can't use C in Objective-C, since Objective-C is C. The term is usually applied when you write code that uses C structures and calls C functions directly, instead of using Objective-C objects and messages.
You can use something like the following:
SEL selector = NSSelectorFromString(methodName);
[myObject performSelector:selector];
There are also performSelector:withObject:
, and performSelector:withObject:withObject:
methods if you need to pass parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With