3

Why use NSInvocation when you could just call the method? Need to ask this question because I can't understand the need to use this class. What is the advantage of using it over just calling the method like below.

 [myArray addObject:myString];

Additional info: What specific challenges does it resolve/address which can't be addressed by the other method calling options.

naz
  • 1,478
  • 2
  • 21
  • 32
  • 1
    when you don't know what to call at compile time. i.e. unknown selector, unknown number of arguments, unknown return type. – Bryan Chen May 08 '14 at 02:55
  • why would I not know? I should know the methods of a class first hand when I develop right? Perhaps there is something else when you say there are unknowns...would appreciate an example scenario. tnx – naz May 08 '14 at 03:05
  • there are a few Ruby/Python/Javascript/Lua to ObjC binding frameworks. you can call method using scripting language, which you won't know anything in compile time – Bryan Chen May 08 '14 at 03:08
  • possible duplicate of [NSInvocation for Dummies?](http://stackoverflow.com/questions/313400/nsinvocation-for-dummies) – Brandon May 08 '14 at 03:57
  • that or the answers here are not yet answering my questions so it would not be a duplicate. Of course I read that post already. – naz May 08 '14 at 04:28

1 Answers1

3

One use: You could use it to implement a chain of undo actions - where each different action would be a different method call to undo. You would encapsulate each undo action as a method call wrapped in an NSInvocation object.

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378