{"id":489,"date":"2015-03-11T12:30:03","date_gmt":"2015-03-11T11:30:03","guid":{"rendered":"http:\/\/andras.palfi.hu\/?p=489"},"modified":"2015-03-11T12:30:03","modified_gmt":"2015-03-11T11:30:03","slug":"blocks-in-objective-c","status":"publish","type":"post","link":"https:\/\/andras.palfi.hu\/?p=489","title":{"rendered":"Blocks in Objective-C"},"content":{"rendered":"<h1 style=\"text-align: center;\">Blocks in Objective-C<\/h1>\n<h2>Quick overview<\/h2>\n<p><em>Blocks<\/em> are the <em>closure pattern <\/em>implementation in Objective-C.\u00a0Blocks are objects containing code like a method but blocks are standalone objects (not belonging to a specific class\/instance) encapsulating and capturing the code and variables enclosed in the beginning and closing brackets.<\/p>\n<p><strong>You declare a block type or variable this way<\/strong>:<br \/>\nreturnType (<strong>^<\/strong>blockName)(paramType, paramType)<\/p>\n<p><strong>Assign a block to this variable<\/strong>:<br \/>\n<strong>\u00a0 \u00a0 ^<\/strong> returnType (paramType paramName, paramType paramName) {<br \/>\n&#8230; code &#8230;<br \/>\n}<\/p>\n<p><strong>Block as a parameter<\/strong>:<br \/>\n(returnValue (<strong>^<\/strong>)(paramType, paramType))blockName<\/p>\n<p><strong>Call with a block as parameter<\/strong>:<br \/>\n<strong>^<\/strong>\u00a0returnType (paramType\u00a0paramName, paramType\u00a0paramName) {<br \/>\n&#8230; code &#8230;<br \/>\n}<\/p>\n<h2>Some example<\/h2>\n<p>You declare a block variable the following way:<\/p>\n<pre class=\"lang:objc decode:true\" title=\"a block which takes a string and and int as parameter and returns a string\">\/\/ a block which takes a string and and int as parameter and returns a string\nNSString* (^simpleBlockVariable)(NSString*, int) = ^ NSString* (NSString* stringParam, int intParam) {\n    return [NSString stringWithFormat:@\"stringParam %@, intParam: %d\", stringParam, intParam];\n};\n\n\/\/ you can call it a the following way\nNSString* stringResult = simpleBlockVariable(@\"Hello\", 1);<\/pre>\n<p>You can assign a new block to the same variable; you can also avoid the declaration of the return value since the compiler can find it out:<\/p>\n<pre class=\"lang:objc decode:true\">simpleBlockVariable = ^ (NSString* stringParam, int intParam) {\n    return [NSString stringWithFormat:@\"second version: stringParam %@, intParam: %d\", stringParam, intParam];\n}<\/pre>\n<p>Even you can avoid the parameter declaration so you get the simplest form of a block:<\/p>\n<pre class=\"lang:objc decode:true\">void (^simplestBlockVariable)() = ^ {\n    NSLog(@\"simplest block called\");\n};\n \n\/\/ you call it like this   \nsimplestBlockVariable();\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Passing blocks as parameters the syntax change a bit<\/p>\n<pre class=\"lang:objc decode:true \">\/\/ a block with two parameters: a string and a block which takes to parameters (string and int) and returns a string\nNSString*(^blockVariableWithBlockParameter)(NSString*, NSString*(^)(NSString* , int )) = ^ NSString* (NSString* theString, NSString*(^completion)(NSString* , int )) {\n    NSString* aNewString = [NSString stringWithFormat:@\"Incoming string: %@\", theString];\n    NSString* resultString = completion(aNewString, 10);\n    return resultString;\n};\n\n\/\/ you call it the following way    \nNSString* theResult = blockVariableWithBlockParameter(@\"Hello\", ^ NSString* (NSString* stringParam, int intParam) {\n    NSString* resultString = [NSString stringWithFormat:@\"string from the completion: stringParam: %@, intParam: %d\", stringParam, intParam];\n    return resultString;\n});\n\n\/\/ Value of theResult: string from the completion: stringParam: Incoming string: Hello, intParam: 10<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Blocks in Objective-C Quick overview Blocks are the closure pattern implementation in Objective-C.\u00a0Blocks are objects containing code like a method but blocks are standalone objects (not belonging to a specific class\/instance) encapsulating and capturing the code and variables enclosed in the beginning and closing brackets. You declare a block type or variable this way: returnType [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"hide_page_title":"","footnotes":""},"categories":[16,18,3,4,8],"tags":[],"_links":{"self":[{"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/posts\/489"}],"collection":[{"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=489"}],"version-history":[{"count":0,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/posts\/489\/revisions"}],"wp:attachment":[{"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}