1

Is it possible to use NSInvocation for static method calls?

We're using NSInvocation to simplify our method calls in unit tests for non-public methods.

This works great for our instance methods, for which we provide an object and appropriate data to NSInvocation. However, can we do the same with static methods?

user1118321
  • 25,567
  • 4
  • 55
  • 86
Gaurav Sharma
  • 2,680
  • 3
  • 26
  • 36
  • 1
    There are no "static methods" in Objective-C. There are "class methods", which are dynamically dispatched at runtime, not looked up statically at compile time. – user102008 Jun 05 '14 at 19:23

1 Answers1

1

Yes, you can. Assuming your class is MyClass and your method is +(void)myClassMethod:(id)sender;, you can use:

NSMethodSignature *signature = [MyClass methodSignatureForSelector:@selector(myClassMethod:)];

Then you can provide the NSMethodSignature to your NSInvocation when created:

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
Craig Otis
  • 31,257
  • 32
  • 136
  • 234
  • 2
    Note also that the `target` of a class method should be a class object. Something like `invocation.target = [MyClass class]`. – Greg Parker Jun 05 '14 at 19:06