I have a class, call it A that parses some data out in an NSDictionary. That class has a table view, and when a cell is selected, a new class instantiates, let’s call that class B. Class B in turn instantiates class C. I want class C to receive the NSDictionary that was created in A.
Would delegates work? Would it work even though class C isn’t instantiated? If that’s true and it really does not, should I pass the data from A–>B–>C in the init method? Are there better message passing methods in Objective-C?
I think that’s the best plan.
A really shouldn’t have to worry about how B does what it does. The fact that B uses C is none of A’s business. All A needs to know is that B needs the dictionary in order to do it’s thing.
As it happens, B doesn’t care all that much about the dictionary, but the dictionary should be part of B’s job description, which is something like: Take the data you’re given and display it somehow. If B deals with the dictionary by passing it directly to C, that’s fine — not something A should care about.
You could do the same thing with a delegate. A could give B a reference to itself as a data source, and B could eventually pass that on to C. It’s the same process you’d have with the dictionary, so there’s no real advantage in doing that if you can put everything that C needs into the dictionary. You might consider switching to delegation if A doesn’t know in advance exactly what data is needed, or when you want A to be able to somehow customize the behavior of C.