{"id":5861,"date":"2014-04-07T09:41:27","date_gmt":"2014-04-07T09:41:27","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/07\/passing-in-a-custom-selector-implementation-collection-of-common-programming-errors\/"},"modified":"2014-04-07T09:41:27","modified_gmt":"2014-04-07T09:41:27","slug":"passing-in-a-custom-selector-implementation-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/07\/passing-in-a-custom-selector-implementation-collection-of-common-programming-errors\/","title":{"rendered":"Passing in a custom selector implementation-Collection of common programming errors"},"content":{"rendered":"<p>You can certainly use the runtime functions to do something like this,* but I&#8217;d suggest that this is <em>exactly<\/em> the sort of problem that Blocks were introduced to solve. They allow you to pass around a chunk of executable code &#8212; your method can actually accept a Block as an argument and run it.<\/p>\n<p>Here&#8217;s a SSCCE:<\/p>\n<pre><code>#import \n\ntypedef dispatch_block_t GenericBlock;\n\n@interface Albatross : NSObject \n- (void)slapFace:(NSNumber *)n usingFish:(GenericBlock)block;\n@end\n\n@implementation Albatross\n\n- (void)slapFace:(NSNumber *)n usingFish:(GenericBlock)block\n{\n    if( [n intValue] &gt; 2 ){\n        NSLog(@\"Cabbage crates coming over the briny!\");\n    }\n    else {\n        block();    \/\/ Execute the block\n    }\n}\n\n@end\n\nint main(int argc, const char * argv[])\n{\n\n    @autoreleasepool {\n\n        Albatross * p = [Albatross new];\n        [p slapFace:[NSNumber numberWithInt:3] usingFish:^{\n            NSLog(@\"We'd like to see the dog kennels, please.\");\n        }];\n         [p slapFace:[NSNumber numberWithInt:1] usingFish:^{\n            NSLog(@\"Lemon curry?\");\n        }];\n\n    }\n    return 0;\n}\n<\/code><\/pre>\n<p>*Note that using <code>method_setImplementation()<\/code> will change the code that&#8217;s used every time that method is called in the future from anywhere &#8212; it&#8217;s a persistent change.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can certainly use the runtime functions to do something like this,* but I&#8217;d suggest that this is exactly the sort of problem that Blocks were introduced to solve. They allow you to pass around a chunk of executable code &#8212; your method can actually accept a Block as an argument and run it. Here&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5861","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5861"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5861\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}