0

My project is going to be compiled with a bunch of legacy code (as >100 independent methods). All the different legacy methods are going to serve the same purpose in my project, they just do so differently. That is to say, they all have roughly the same inputs and the same outputs, but they do some very complex calculations to get those outputs, and those calculations can vary greatly between the different methods. I expect to only have to call one of the methods during a given session, but the next session may need to call a different method. In my project, when I am about to call one of the methods, the user will have entered enough information for me to be able to determine which method to call.

One way I could handle this would be with a huge if block. I would have to check against a couple different conditions. For each combination of conditions, I would call a different one of my legacy methods. The problem with this approach is that it would be very bad from a readability point of view - the actual if block would probably end up being 100s of lines long. I imagine there could also be some performance issues with that many different else-if statements as well.

What I would like to do is create a look-up table, where I store the name of the method to call (as a string), and pair that with the different conditions I need to check against. My company is trying to shift its policy towards using more look-up tables and fewer of the huge if blocks.

Is there a way to call a method based on the "method name" string that I'm getting back from my look-up table? If so, what would the syntax look like for doing something like that? Or are there other (better) ways to handle this that I haven't thought of? Or is the huge if block actually more in line with best practice?

GeneralMike
  • 2,951
  • 3
  • 28
  • 56
  • Ugh, why did [this link](http://stackoverflow.com/questions/4446883/objective-c-calling-method-dynamically-with-a-string?rq=1) not appear when I was doing my goggling or while I was typing this up? I think this is what I'm looking for - going to try it out, will delete my question and upvote linked question if it works. – GeneralMike Jun 24 '14 at 20:58
  • performSelector may work, one of it's limitations though is that you can't attach as many parameters as you may need. – Brandon Jun 24 '14 at 22:46

1 Answers1

0

NSInvocation might work for your needs. Essentially it allows you to create a method invocation from a selector and then call it later on. You can use the NSSelectorFromString to create a selector based on the string you looked up. There is an excellent tutorial on NSInvocation I bookmarked a while ago http://a-coding.com/making-nsinvocations/.

I'm not sure of other ways to accomplish what your going for but I can imagine this approach getting difficult to debug and track, though sometimes you never know until you implement it.

I used NSInvocation once to put an app into 'demo' mode. I created a whole array of NSSInvocations and then whenever my timer function fired I would grab the next invocation and invoke it, allow my app to act like someone was using it. Anyways good luck.

Brandon
  • 2,387
  • 13
  • 25