ios - Better pattern than passing NSDictionaries as parameters? -
ios - Better pattern than passing NSDictionaries as parameters? -
as our codebase matures i'm starting not pattern of passing dictionaries way bundle info message passing or, worse yet, function arguments. necessitates sending , receiving function both having undocumented api of string literals.
..in function.. nsdictionary *info = [nsdictionary dictionarywithobjectsandkeys: thisobject, @"thiskey", thatobject, @"thatkey", nil]; [[nsnotificationcenter defaultcenter] postnotificationname:@"my_notification" object:nil userinfo:info]; ....
and in someclass
's listener
- (void)someclasslistener:(nsnotification *)notification { nsdictionary *info = [notification userinfo]; thisobject *ob1 = [info objectforkey:@"thiskey"]; thatobject *ob2 = [info objectforkey:@"thatkey"]; }
you have remember thiskey
, thatkey
keys of type thisobject
, thatobject
notification, sure create constants somewhere keys doesn't solve problem.
and lets have function needs 15 arguments, you're not going create function 15 parameters, it'd much easier (though less readable) pass dictionary have same problem above.
i played creating stubbed out 'message classes' in class' header files (ie 2 interfaces in 1 header) , message class list of objects define , send method creates stronger contract feels wrong.
it'd great if typedef
parameter object in header doesn't back upwards nsobject
's things int
or float
etc.
essentially i'm trying create stronger contract between message sender , message receiver, functions or notifications.
you define constants keys. example, see docs uikeyboarddidshownotification
example. there link of keys can used info notification.
a improve approach encapsulate info class instead of dictionary. create simple class properties. much more self documenting dictionary. can see property names , property types in .h file.
if find have methods needing 15 arguments, need step , encapsulate arguments appropriate class. perhaps method reduces couple of arguments , class or similar.
ios objective-c osx model-view-controller design-patterns
Comments
Post a Comment