Note that there are some explanatory texts on larger screens.

plurals
  1. POBetter pattern than passing NSDictionaries as parameters?
    primarykey
    data
    text
    <p>As our codebase matures I'm starting to not like the pattern of passing dictionaries as a way to package up information for message passing or, worse yet, function arguments. It necessitates the sending and receiving function both having an undocumented API of string literals.</p> <pre><code>..in some function.. NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys: thisObject, @"thisKey", thatObject, @"thatKey", nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"MY_NOTIFICATION" object:nil userInfo:info]; .... </code></pre> <p>and then in <code>someClass</code>'s listener</p> <pre><code>- (void)someClassListener:(NSNotification *)notification { NSDictionary *info = [notification userInfo]; ThisObject *ob1 = [info objectForKey:@"thisKey"]; ThatObject *ob2 = [info objectForKey:@"thatKey"]; } </code></pre> <p>You have to remember that <code>thisKey</code> and <code>thatKey</code> are the keys of type <code>ThisObject</code> and <code>ThatObject</code> for that notification, sure you could create some constants somewhere for those keys but that doesn't really solve the problem.</p> <p>And lets say you have a function that needs 15 arguments, you're not going to make a function with 15 parameters, it'd be much easier (though less readable) to just pass a dictionary but now you have the same problem as above.</p> <p>I played with creating stubbed out 'Message Classes' in those class' header files (ie two interfaces in one header) and the Message Class was just a list of objects that you define and send to the method which creates a stronger contract but this feels Wrong.</p> <p>It'd be great if I could do something like <code>typeDef</code> a parameter object in the header but that doesn't support <code>NSObject</code>'s only things like <code>int</code> or <code>float</code> etc.</p> <p><strong>Essentially I'm trying to create a stronger contract between the message sender and message receiver, be that functions or notifications.</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload