{"id":269330,"date":"2015-11-30T20:43:01","date_gmt":"2015-11-30T17:43:01","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=269330"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=269330","title":{"rendered":"Objective-C \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0431\u043b\u043e\u043a\u0438"},"content":{"rendered":"<p>       \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0431\u043b\u043e\u043a\u043e\u0432 (__NSStackBlock__\/__NSGlobalBlock__\/__NSMallocBlock__), \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0437\u0430\u0445\u0432\u0430\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0438 \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0432\u043e \u0447\u0442\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0431\u043b\u043e\u043a. <\/p>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u043e\u0432 \u0432 Objective-C \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u043d\u0435\u0439 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430. \u041d\u043e \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043d\u0435 \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u044e\u0442\u0441\u044f, \u043a\u0430\u043a \u0431\u043b\u043e\u043a\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0432\u043d\u0443\u0442\u0440\u0438. \u0417\u0434\u0435\u0441\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043c\u0430\u0433\u0438\u0438, \u044f \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e\u0431 \u044d\u0442\u043e\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.<\/p>\n<p>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0430\u0447\u0430\u043b\u0430, \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0431\u043b\u043e\u043a \u0432 Objective-C  <\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\"  src=\"https:\/\/habrastorage.org\/files\/3a4\/d0c\/c31\/3a4d0cc31edc48d5aca9095dd3be8b4b.jpg\"\/><\/div>\n<p>  \u0414\u043b\u044f \u0447\u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0431\u043b\u043e\u043a\u0438 \u044f \u0440\u0430\u0441\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0443, \u0440\u0435\u0447\u044c \u0438\u0434\u0435\u0442 \u043d\u0435 \u043e\u0431 \u044d\u0442\u043e\u043c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0440\u0430\u0437\u0443 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435.<br \/>  <a name=\"habracut\"><\/a><\/p>\n<p>  \u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0431\u043b\u043e\u043a? \u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0431\u043b\u043e\u043a \u2014 \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442.   <\/p>\n<pre><code class=\"objectivec\">id thisIsBlock = ^{ }; <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043e\u0431\u044a\u0435\u043a\u0442  <\/p>\n<pre><code class=\"objectivec\">struct objc_object {     Class isa  OBJC_ISA_AVAILABILITY; };  \/\/\/ A pointer to an instance of a class. typedef struct objc_object *id; <\/code><\/pre>\n<p>  \u0410 \u0443 \u0431\u043b\u043e\u043a\u0430 \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434   <\/p>\n<pre><code class=\"objectivec\">- (Class)class <\/code><\/pre>\n<p> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 isa<\/p>\n<p>  \u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u0437\u043d\u0430\u0435\u043c \u0442\u0435\u043f\u0435\u0440\u044c \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0438\u043c\u0435\u0435\u0442 \u0431\u043b\u043e\u043a \u0432 \u043a\u0430\u043a\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">__NSStackBlock__<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"objectivec\">    int foo = 3;     Class class = [^{         int foo1 = foo + 1;     } class];     NSLog(@&quot;%@&quot;, NSStringFromClass(class)); <\/code><\/pre>\n<p>  2015-11-29 22:30:21.054 block_testing[99727:13189641] __NSStackBlock__  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">__NSMallocBlock__<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"objectivec\">    int foo = 3;     Class class = [[^{         int foo1 = foo + 1;     } copy] class];     NSLog(@&quot;%@&quot;, NSStringFromClass(class)); <\/code><\/pre>\n<p>  2015-11-29 22:33:45.026 block_testing[99735:13190778] __NSMallocBlock__  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">__NSGlobalBlock__<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"objectivec\">    Class class = [^{     } class];     NSLog(@&quot;%@&quot;, NSStringFromClass(class)); <\/code><\/pre>\n<p>  2015-11-29 22:34:49.645 block_testing[99743:13191389] __NSGlobalBlock__  <\/div>\n<\/div>\n<p>  \u041d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0432\u0430\u0440\u0438\u0430\u043d\u0442 __NSMallocBlock__  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">ARC<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"objectivec\">    int foo = 3;     id thisIsBlock = ^{         int foo1 = foo + 1;     };     Class class = [thisIsBlock class];     NSLog(@&quot;%@&quot;, NSStringFromClass(class)); <\/code><\/pre>\n<p>  2015-11-29 22:37:27.638 block_testing[99751:13192462] __NSMallocBlock__  <\/div>\n<\/div>\n<p>  \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u0435\u0441\u043b\u0438 \u0431\u043b\u043e\u043a \u043d\u0435 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0442\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c __NSGlobalBlock__  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u042d\u043a\u0441\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u0442\u044b<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"objectivec\">    Class class = [[^{     } copy] class];     NSLog(@&quot;%@&quot;, NSStringFromClass(class)); <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"objectivec\">    id thisIsBlock = ^{     };     Class class = [thisIsBlock class];     NSLog(@&quot;%@&quot;, NSStringFromClass(class)); <\/code><\/pre>\n<p>  __NSGlobalBlock__  <\/div>\n<\/div>\n<p>  \u0415\u0441\u043b\u0438 \u0436\u0435 \u0431\u043b\u043e\u043a \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0442\u043e \u0431\u043b\u043e\u043a __NSStackBlock__ (\u043d\u0430 \u0441\u0442\u0435\u043a\u0435). \u041e\u0434\u043d\u0430\u043a\u043e \u0435\u0441\u043b\u0438 \u0436\u0435 \u043f\u043e\u0441\u043b\u0430\u0442\u044c \u0431\u043b\u043e\u043a\u0443 &#8216;copy&#8217;, \u0442\u043e \u0431\u043b\u043e\u043a \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u0441\u044f \u0432 \u043a\u0443\u0447\u0443 (__NSMallocBlock__).<br \/>  ARC \u043d\u0430\u043c \u0432 \u044d\u0442\u043e\u043c \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442, \u0438 \u043f\u0440\u0438 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0430 \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e (__strong) \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435. \u0412 \u043e\u0431\u0449\u0435\u043c, \u0435\u0449\u0435 \u0440\u0430\u0437 \u0441\u043a\u0430\u0436\u0435\u043c ARC \u0441\u043f\u0430\u0441\u0438\u0431\u043e, \u0432\u0435\u0434\u044c \u0432 MRC \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u0440\u0430\u0439\u043d\u0435 \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u044b\u0435 \u0431\u0430\u0433\u0438  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">object.h<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"objectivec\">\/*!  * @typedef dispatch_block_t  *  * @abstract  * The type of blocks submitted to dispatch queues, which take no arguments  * and have no return value.  *  * @discussion  * When not building with Objective-C ARC, a block object allocated on or  * copied to the heap must be released with a -[release] message or the  * Block_release() function.  *  * The declaration of a block literal allocates storage on the stack.  * Therefore, this is an invalid construct:  * &lt;code&gt;  * dispatch_block_t block;  * if (x) {  *     block = ^{ printf(&quot;true\\n&quot;); };  * } else {  *     block = ^{ printf(&quot;false\\n&quot;); };  * }  * block(); \/\/ unsafe!!!  * &lt;\/code&gt;  *  * What is happening behind the scenes:  * &lt;code&gt;  * if (x) {  *     struct Block __tmp_1 = ...; \/\/ setup details  *     block = &__tmp_1;  * } else {  *     struct Block __tmp_2 = ...; \/\/ setup details  *     block = &__tmp_2;  * }  * &lt;\/code&gt;  *  * As the example demonstrates, the address of a stack variable is escaping the  * scope in which it is allocated. That is a classic C bug.  *  * Instead, the block literal must be copied to the heap with the Block_copy()  * function or by sending it a -[copy] message.  *\/ typedef void (^dispatch_block_t)(void); <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0414\u043b\u044f \u0447\u0435\u0433\u043e \u0442\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u043f\u0435\u0440\u0442\u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442\u0441\u044f \u043a\u0430\u043a &#8216;copy&#8217;?  <\/p>\n<blockquote><p>Note: You should specify copy as the property attribute, because a block needs to be copied to keep track of its captured state outside of the original scope. This isn\u2019t something you need to worry about when using Automatic Reference Counting, as it will happen automatically, but it\u2019s best practice for the property attribute to show the resultant behavior. For more information, see Blocks Programming Topics.  <\/p><\/blockquote>\n<p><a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/Cocoa\/Conceptual\/ProgrammingWithObjectiveC\/WorkingwithBlocks\/WorkingwithBlocks.html#\/\/apple_ref\/doc\/uid\/TP40011210-CH8-SW1\">\u0441\u0441\u044b\u043b\u043a\u0430<\/a><br \/>  \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0439\u0442\u0435 \u0438 \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u0438\u0441\u0430\u0442\u044c property copy \u0434\u043b\u044f \u0431\u043b\u043e\u043a\u043e\u0432. <\/p>\n<p>  \u0422\u0430\u043a \u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043e __NSStackBlock__   <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c __NSStackBlock__ \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e?<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"objectivec\">typedef void(^EmptyBlock)(); void fooFunc(EmptyBlock block) {     Class class = [block class];     NSLog(@&quot;%@&quot;, NSStringFromClass(class)); }  int main(int argc, const char * argv[]) {     @autoreleasepool {         int foo = 1;         fooFunc(^{             int foo1 = foo + 1;         });     }     return 0; } <\/code><\/pre>\n<p>  2015-11-29 22:52:16.905 block_testing[99800:13197825] __NSStackBlock__  <\/div>\n<\/div>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0417\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u0435\u043d __NSGlobalBlock__<\/b><\/p>\n<div class=\"spoiler_text\">\u041e __NSStackBlock__ \u0438 __NSMallocBlock__ \u0431\u044b\u043b\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043c\u043d\u043e\u0433\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438, \u043a\u0430\u043a\u043e\u0439 \u0442\u043e\u043b\u043a \u043e\u0442 __NSGlobalBlock__? \u0412\u043d\u0443\u0442\u0440\u0438 \u0442\u0430\u043a\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430 \u043d\u0435\u0442 \u0437\u0430\u0445\u0432\u0430\u0442\u0430 \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0439 \u0438\u0437-\u0437\u0430 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0430\u043c\u044f\u0442\u0438, \u043a\u0430\u043a \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 __NSStackBlock__. \u0418 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u043f\u0435\u0440\u0432\u0430 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0431\u043b\u043e\u043a \u043d\u0430 \u0441\u0442\u0435\u043a\u0435, \u043f\u043e\u0442\u043e\u043c \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442\u044c \u0432 \u043a\u0443\u0447\u0443, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0443 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0449\u0435.  <\/div>\n<\/div>\n<p>  \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u0431\u044b\u043b\u043e \u0441\u043a\u0430\u0437\u0430\u043d\u043e \u043e \u0437\u0430\u0445\u0432\u0430\u0442\u0435 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u043e\u0434\u043d\u0430\u043a\u043e \u043c\u044b \u0432\u0441\u0435 \u0435\u0449\u0435 \u043d\u0435 \u0441\u043a\u0430\u0437\u0430\u043b\u0438, \u043a\u0430\u043a \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442. \u0412 \u044d\u0442\u043e\u043c \u043d\u0430\u043c \u043f\u043e\u043c\u043e\u0436\u0435\u0442 <a href=\"http:\/\/clang.llvm.org\/docs\/Block-ABI-Apple.html\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f clang<\/a>. \u041d\u0435 \u0431\u0443\u0434\u0435\u043c \u0435\u0435 \u0440\u0430\u0441\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e, \u0432\u044b\u0434\u0435\u043b\u0438\u043c \u043b\u0438\u0448\u044c \u043d\u0443\u0436\u043d\u044b\u0435 \u0438\u0434\u0435\u0438.<br \/>  \u0411\u043b\u043e\u043a \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443, \u0430 \u0437\u0430\u0445\u0432\u0430\u0442\u044b \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u0432 \u043f\u043e\u043b\u044f.<br \/>  \u042d\u0442\u043e \u043d\u0435 \u0441\u043b\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438   <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0441\u043d\u043e\u0441\u043a\u0430 \u0434\u043b\u044f \u0446\u0435\u043d\u0438\u0442\u0435\u043b\u0435\u0439<\/b><\/p>\n<div class=\"spoiler_text\">\n<blockquote><p>clang -rewrite-objc -ObjC main.m -o out.cpp<\/p><\/blockquote>\n<p>  \u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0441\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432 C++ \u043a\u043e\u0434\u0435  <\/div>\n<\/div>\n<p>  <\/p>\n<pre><code class=\"objectivec\">    MyObject *myObject = [[MyObject alloc] init];     NSLog(@&quot;object %p, ptr myObject %p&quot;, myObject, &myObject);     ^{         NSLog(@&quot;object %p, ptr myObject %p&quot;, myObject, &myObject);     }(); <\/code><\/pre>\n<p>  2015-11-29 23:12:37.297 block_testing[99850:13203592] object 0x100111e10, ptr myObject 0x7fff5fbff798<br \/>  2015-11-29 23:12:37.298 block_testing[99850:13203592] object 0x100111e10, ptr myObject 0x7fff5fbff790<\/p>\n<p>  \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c myObject \u0441\u043d\u0430\u0440\u0443\u0436\u0438 \u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0431\u043b\u043e\u043a\u0430 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u0438, \u043e\u0434\u043d\u0430\u043a\u043e \u0441\u0430\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f.<br \/>  \u0411\u043b\u043e\u043a \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442. \u0427\u0442\u043e \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442 \u043a\u043e\u043b-\u0432\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439, \u043f\u043e \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0434\u043b\u044f ARC \u043b\u043e\u0433\u0438\u043a\u0435 (\u043f\u0440\u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0431\u043b\u043e\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443).<br \/>  \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 __block \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 inout, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a \u043d\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c\u0441\u044f (\u043e\u0434\u043d\u0430\u043a\u043e \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435\u0433\u0434\u0430 \u0438 \u0432\u0435\u0437\u0434\u0435, const \u2014 \u044d\u0442\u043e \u0445\u043e\u0440\u043e\u0448\u0435\u0435 \u0441\u043b\u043e\u0432\u043e)<\/p>\n<p>  \u0414\u043b\u044f \u0437\u0430\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u0438\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u043a\u043e\u0434<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"objectivec\">#import &lt;Foundation\/Foundation.h&gt;   typedef NSInteger (^IncrementBlock)(); IncrementBlock createIncrementBlock(const NSInteger start, const NSInteger incrementValue) {     __block NSInteger acc = start;     return ^NSInteger{         acc += incrementValue;         return acc;     }; }  int main(int argc, const char * argv[]) {     @autoreleasepool {         IncrementBlock incrementBlock = createIncrementBlock(0, 2);         NSLog(@&quot;%ld&quot;, incrementBlock());         NSLog(@&quot;%ld&quot;, incrementBlock());         NSLog(@&quot;%ld&quot;, incrementBlock());                  IncrementBlock incrementBlock1 = createIncrementBlock(0, 2);         NSLog(@&quot;%ld&quot;, incrementBlock1());         NSLog(@&quot;%ld&quot;, incrementBlock1());         NSLog(@&quot;%ld&quot;, incrementBlock1());     }     return 0; } <\/code><\/pre>\n<p>  2015-11-29 23:31:24.027 block_testing[99910:13209611] 2<br \/>  2015-11-29 23:31:24.028 block_testing[99910:13209611] 4<br \/>  2015-11-29 23:31:24.028 block_testing[99910:13209611] 6<br \/>  2015-11-29 23:31:24.028 block_testing[99910:13209611] 2<br \/>  2015-11-29 23:31:24.028 block_testing[99910:13209611] 4<br \/>  2015-11-29 23:31:24.029 block_testing[99910:13209611] 6  <\/div>\n<\/div>\n<p>  \u0411\u043b\u043e\u043a IncrementBlock \u0431\u044b\u043b \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0435\u043d \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443, \u043f\u0440\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0435 \u0431\u043b\u043e\u043a\u0430 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043e\u0431\u043b\u0430\u0441\u0442\u0438, \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0435\u0441\u0442\u044c \u043f\u043e\u043b\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0430\u043a\u043a\u0443\u043c\u0443\u043b\u044f\u0442\u043e\u0440. \u0410 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 createIncrementBlock \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440.<\/p>\n<p>  \u041e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u044e \u0442\u0430\u043a \u0436\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f self \u0432\u043d\u0443\u0442\u0440\u0438 \u0431\u043b\u043e\u043a\u0430 (<a href=\"http:\/\/habrahabr.ru\/post\/270913\/\">\u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 self<\/a>). \u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438 \u0434\u043e\u043b\u0436\u043d\u043e \u0441\u0442\u0430\u0442\u044c \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 self \u0432\u043d\u0443\u0442\u0440\u0438 __NSMallocBlock__ \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0441\u0441\u044b\u043b\u043e\u043a, \u043e\u0434\u043d\u0430\u043a\u043e \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 retain cycle. retain cycle \u044d\u0442\u043e \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u044a\u0435\u043a\u0442 \u0434\u0435\u0440\u0436\u0438\u0442 \u0431\u043b\u043e\u043a, \u0430 \u0431\u043b\u043e\u043a \u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0435\u0440\u0436\u0438\u0442 \u0431\u043b\u043e\u043a\u2026<br \/>  \u041f\u0430\u0440\u0430\u043d\u043e\u044f \u0432\u0435\u0437\u0434\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c __weak __strong \u043d\u0435 \u043d\u0443\u0436\u043d\u0430, \u0434\u043b\u044f \u043d\u0430\u0441 \u0431\u043b\u043e\u043a \u2014 \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442. \u041f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c, \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u044c\u044e.               <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/271255\/\"> http:\/\/habrahabr.ru\/post\/271255\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0431\u043b\u043e\u043a\u043e\u0432 (__NSStackBlock__\/__NSGlobalBlock__\/__NSMallocBlock__), \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0437\u0430\u0445\u0432\u0430\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0438 \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0432\u043e \u0447\u0442\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0431\u043b\u043e\u043a. <\/p>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u043e\u0432 \u0432 Objective-C \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u043d\u0435\u0439 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430. \u041d\u043e \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043d\u0435 \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u044e\u0442\u0441\u044f, \u043a\u0430\u043a \u0431\u043b\u043e\u043a\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0432\u043d\u0443\u0442\u0440\u0438. \u0417\u0434\u0435\u0441\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043c\u0430\u0433\u0438\u0438, \u044f \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e\u0431 \u044d\u0442\u043e\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.<\/p>\n<p>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0430\u0447\u0430\u043b\u0430, \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0431\u043b\u043e\u043a \u0432 Objective-C  <\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\"  src=\"https:\/\/habrastorage.org\/files\/3a4\/d0c\/c31\/3a4d0cc31edc48d5aca9095dd3be8b4b.jpg\"\/><\/div>\n<p>  \u0414\u043b\u044f \u0447\u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0431\u043b\u043e\u043a\u0438 \u044f \u0440\u0430\u0441\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0443, \u0440\u0435\u0447\u044c \u0438\u0434\u0435\u0442 \u043d\u0435 \u043e\u0431 \u044d\u0442\u043e\u043c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0440\u0430\u0437\u0443 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-269330","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/269330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=269330"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/269330\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=269330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=269330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=269330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}