{"id":1562,"date":"2013-01-16T08:52:55","date_gmt":"2013-01-16T16:52:55","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1562"},"modified":"2013-01-16T08:52:55","modified_gmt":"2013-01-16T16:52:55","slug":"using-an-nsdictionary-as-an-argument-to-a-method","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1562","title":{"rendered":"Using an NSDictionary as an argument to a method."},"content":{"rendered":"<p>I have a series of animated scenes that I am adding as rewards to my apps. The animation takes place on three layers at the moment, but I keep adding layers and every time I do that I have to change my method definition and all the method calls. I decided to put all my arguments into an NSDictionary so that I don\u2019t have to keep changing the method.<\/p>\n<p>This is the original code.<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nNSInteger numRewards = 10;\nNSInteger randomReward = arc4random() % numRewards;\n\nif (randomReward == 0 ) {\n&nbsp;&nbsp;&nbsp;&nbsp;UIColor *backgroundColor = [UIColor colorWithRed:102\/255.0 green:204\/255.0 blue:255\/255.0 alpha:1];\n&nbsp;&nbsp;&nbsp;&nbsp;[self animatedReward:@&quot;Boat&quot;\n&nbsp;&nbsp;&nbsp;&nbsp; withForegroundCount:4\n&nbsp;&nbsp;&nbsp;&nbsp;withMovingPartsCount:6\n&nbsp;&nbsp;&nbsp;&nbsp; withBackgroundColor:backgroundColor];\n}\n<\/code><\/pre><\/p>\n<p>Every time I add a layer I need to change the method. And all the method calls. By making it an NSDictionary, I can play around with layers, and anything else I want to add, without having to change the method each time.<\/p>\n<p><pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nNSInteger numRewards = 10;\nNSInteger randomReward = arc4random() % numRewards;\n\nif (randomReward == 0 ) {\n&nbsp;&nbsp;&nbsp;&nbsp;UIColor *backgroundColor = [UIColor colorWithRed:102\/255.0 green:204\/255.0 blue:255\/255.0 alpha:1];\n&nbsp;&nbsp;&nbsp;&nbsp;NSDictionary *partsCounts = [NSDictionary dictionaryWithObjectsAndKeys:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [NSNumber numberWithInt:4],@&quot;foreground&quot;,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [NSNumber numberWithInt:3],@&quot;moving&quot;, nil];\n&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@&quot;Parts counts%i&quot;,partsCount);\n&nbsp;&nbsp;&nbsp;&nbsp;[self animatedReward:@&quot;Boat&quot;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; partsCounts:partsCounts\n&nbsp;&nbsp;&nbsp;&nbsp; withBackgroundColor:backgroundColor];\n}\n<\/code><\/pre><\/p>\n<p>The method is declared as:<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\n- (void)animatedReward:(NSString *)scene\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; partsCounts:(NSDictionary)partsCounts\n&nbsp;&nbsp; withBackgroundColor:(UIColor *)backgroundColor;\n<\/code><\/pre><\/p>\n<p>Notice that the number of images in each layer are stored as NSNumbers in the dictionary. That\u2019s because NSIntegers aren\u2019t objects. To get the integers back out of the dictionary, you use this line.<\/p>\n<p><pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\n&nbsp;&nbsp;&nbsp;&nbsp;NSInteger foregroundCount = [[partsCounts objectForKey:@&quot;foreground&quot;]&nbsp;&nbsp;integerValue];\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a series of animated scenes that I am adding as rewards to my apps. The animation takes place on three layers at the moment, but I keep adding layers and every time I do that I have to change my method definition and all the method calls. I decided to put all my &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1562\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using an NSDictionary as an argument to a method.<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-1562","post","type-post","status-publish","format-standard","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1562"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1562\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}