{"id":224363,"date":"2014-05-29T13:58:02","date_gmt":"2014-05-29T09:58:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=224363"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=224363","title":{"rendered":"<span class=\"post_title\">\u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u0431\u043b\u043e\u043a\u043e\u0432 \u0432 Objective-C<\/span>"},"content":{"rendered":"<div class=\"content html_format\">     \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/736\/611\/3e8\/7366113e881ae5be22641494e257b2ea.jpg\" width=\"600\" alt=\"image\"\/><br \/>  \u0412 Objective-C \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u0430\u044f \u0448\u0442\u0443\u043a\u0430, \u043a\u0430\u043a \u0431\u043b\u043e\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438 <a href=\"http:\/\/ru.wikipedia.org\/wiki\/%D0%97%D0%B0%D0%BC%D1%8B%D0%BA%D0%B0%D0%BD%D0%B8%D0%B5_(%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5)\">\u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0439<\/a>.<\/p>\n<p>  \u0415\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0435\u0439 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u043b\u043e\u043a\u0438 (\u043a\u043e\u0433\u0434\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c copy, \u043a\u0430\u043a \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 retain \u0446\u0438\u043a\u043b\u043e\u0432 \u0438 \u0442. \u0434.), \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0431\u043b\u043e\u043a\u043e\u0432 \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435 \u0437\u0430\u0442\u0440\u0430\u0433\u0438\u0432\u0430\u044e\u0442. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u043d\u0438\u043c \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u0431\u0435\u043b.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b<\/h4>\n<p>  \u041d\u0435 \u0432\u0441\u0435 \u0437\u043d\u0430\u044e\u0442, \u043d\u043e \u0432 \u043a\u043b\u044d\u043d\u0433\u0435 \u0435\u0441\u0442\u044c \u043e\u043f\u0446\u0438\u044f -rewrite-objc, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u043a\u043e\u0434 \u0441 Objective-C \u043d\u0430 C++. \u0418\u043c\u0435\u043d\u043d\u043e \u043d\u0430 C++, \u0430 \u043d\u0435 C \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043f\u043e\u043c\u0438\u043c\u043e Obective-C \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0438 Objective-C++.<\/p>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:  <\/p>\n<pre><code class=\"bash\">clang -rewrite-objc -ObjC main.m -o out.cpp <\/code><\/pre>\n<p>  \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0438\u0434\u0435\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u044d\u0442\u0443 \u043e\u043f\u0446\u0438\u044e, \u043f\u043e\u043d\u044f\u0442\u044c, \u0432\u043e \u0447\u0442\u043e \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u0431\u043b\u043e\u043a\u0438, \u0438 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430.<\/p>\n<h4>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u043c \u043a\u043e\u0434<\/h4>\n<p>  \u0418\u0442\u0430\u043a, \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:  <\/p>\n<pre><code class=\"objectivec\">#import &lt;Foundation\/Foundation.h&gt;  typedef int (^blk_t)(int intVar);  int main(int argc, const char * argv[]) {     __block NSString *blockString;     NSString *string;          blk_t blk = ^(int intVar) {         blockString = @&quot;Hello&quot;;         NSLog(@&quot;%@&quot;, string);         return intVar;     };          blk(0);        return 0; } <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a (\u043d\u0435\u0437\u043d\u0430\u0447\u0438\u043c\u044b\u0435 \u0447\u0430\u0441\u0442\u0438 \u043e\u043f\u0443\u0449\u0435\u043d\u044b):  <\/p>\n<pre><code class=\"cpp\">#ifndef BLOCK_IMPL #define BLOCK_IMPL struct __block_impl {   void *isa;   int Flags;   int Reserved;   void *FuncPtr; }; \/\/ Runtime copy\/destroy helper functions (from Block_private.h) #ifdef __OBJC_EXPORT_BLOCKS extern &quot;C&quot; __declspec(dllexport) void _Block_object_assign(void *, const void *, const int); extern &quot;C&quot; __declspec(dllexport) void _Block_object_dispose(const void *, const int); extern &quot;C&quot; __declspec(dllexport) void *_NSConcreteGlobalBlock[32]; extern &quot;C&quot; __declspec(dllexport) void *_NSConcreteStackBlock[32]; #else __OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int); __OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int); __OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32]; __OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32]; #endif #endif #define __block #define __weak  typedef int (*blk_t)(int intVar);  struct __Block_byref_blockString_0 {   void *__isa; __Block_byref_blockString_0 *__forwarding;  int __flags;  int __size;  void (*__Block_byref_id_object_copy)(void*, void*);  void (*__Block_byref_id_object_dispose)(void*);  NSString *blockString; };  struct __main_block_impl_0 {   struct __block_impl impl;   struct __main_block_desc_0* Desc;   NSString *string;   __Block_byref_blockString_0 *blockString; \/\/ by ref   __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, NSString *_string, __Block_byref_blockString_0 *_blockString, int flags=0) : string(_string), blockString(_blockString-&gt;__forwarding) {     impl.isa = &_NSConcreteStackBlock;     impl.Flags = flags;     impl.FuncPtr = fp;     Desc = desc;   } }; static int __main_block_func_0(struct __main_block_impl_0 *__cself, int intVar) {   __Block_byref_blockString_0 *blockString = __cself-&gt;blockString; \/\/ bound by ref   NSString *string = __cself-&gt;string; \/\/ bound by copy          (blockString-&gt;__forwarding-&gt;blockString) = (NSString *)&__NSConstantStringImpl__var_folders_v1_v8wjvjd96vl0nhqsjtxs_c2h0000gn_T_main_1cea37_mi_0;         NSLog((NSString *)&__NSConstantStringImpl__var_folders_v1_v8wjvjd96vl0nhqsjtxs_c2h0000gn_T_main_1cea37_mi_1, string);         return intVar;     } static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst-&gt;blockString, (void*)src-&gt;blockString, 8\/*BLOCK_FIELD_IS_BYREF*\/);_Block_object_assign((void*)&dst-&gt;string, (void*)src-&gt;string, 3\/*BLOCK_FIELD_IS_OBJECT*\/);}  static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src-&gt;blockString, 8\/*BLOCK_FIELD_IS_BYREF*\/);_Block_object_dispose((void*)src-&gt;string, 3\/*BLOCK_FIELD_IS_OBJECT*\/);}  static struct __main_block_desc_0 {   size_t reserved;   size_t Block_size;   void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);   void (*dispose)(struct __main_block_impl_0*); } __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0}; int main(int argc, const char * argv[]) {     __attribute__((__blocks__(byref))) __Block_byref_blockString_0 blockString = {(void*)0,(__Block_byref_blockString_0 *)&blockString, 33554432, sizeof(__Block_byref_blockString_0), __Block_byref_id_object_copy_131, __Block_byref_id_object_dispose_131}; ;     NSString *string;      blk_t blk = (int (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, string, (__Block_byref_blockString_0 *)&blockString, 570425344);      ((int (*)(__block_impl *, int))((__block_impl *)blk)-&gt;FuncPtr)((__block_impl *)blk, 0);      return 0; } <\/code><\/pre>\n<h4>\u0427\u0442\u043e \u0436\u0435 \u0442\u0430\u043a\u043e\u0435 \u0431\u043b\u043e\u043a<\/h4>\n<p>  \u041d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 C++ \u043a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0437\u0430\u043f\u0443\u0442\u0430\u043d\u043d\u043e, \u043d\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0430:  <\/p>\n<pre><code class=\"cpp\">   blk_t blk = (int (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, string, (__Block_byref_blockString_0 *)&blockString, 570425344); <\/code><\/pre>\n<p>  \u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0430 \u0435\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e (\u0434\u0430, \u0432 C++ \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u0430\u044f \u0448\u0442\u0443\u043a\u0430) \u0438 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u0442 \u0435\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439, \u0445\u0440\u0430\u043d\u044f\u0449\u0435\u0439 \u0431\u043b\u043e\u043a. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u044b\u0445\u043e\u0434\u0438\u0442, \u0447\u0442\u043e \u0431\u043b\u043e\u043a \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439.<\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 \u044d\u0442\u0443 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443.  <\/p>\n<pre><code class=\"cpp\">struct __block_impl {   void *isa;   int Flags;   int Reserved;   void *FuncPtr; };  struct __main_block_impl_0 {   struct __block_impl impl;   struct __main_block_desc_0* Desc;   NSString *string;   __Block_byref_blockString_0 *blockString; \/\/ by ref    __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, NSString *_string, __Block_byref_blockString_0 *_blockString, int flags=0) : string(_string), blockString(_blockString-&gt;__forwarding)  {     impl.isa = &_NSConcreteStackBlock;     impl.Flags = flags;     impl.FuncPtr = fp;     Desc = desc;   } }; <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 __block_impl \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0441\u043d\u043e\u0432\u043e\u0439 \u0434\u043b\u044f \u0431\u043b\u043e\u043a\u0430. \u0418 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u043e\u043b\u0435 isa, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0443 \u043d\u0430 Objective-C.<br \/>  \u041d\u0430 \u0432\u0441\u044f\u043a\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u043d\u0430\u043f\u043e\u043c\u043d\u044e: \u043a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 Objective-C \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 isa \u2014 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 class object \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<\/p>\n<p>  \u041e\u0441\u043d\u043e\u0432\u0430 \u043b\u044e\u0431\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0432 Objective-C \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cpp\">typedef struct objc_class *Class;  struct objc_object {     Class isa ; }; <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043c \u043a \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u0432\u044b\u0432\u043e\u0434\u0443 \u2014 \u0431\u043b\u043e\u043a\u0438 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438! <br \/>  \u0425\u043e\u0442\u044f, \u0447\u0435\u0433\u043e \u0442\u0443\u0442 \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u043e\u0433\u043e, \u0435\u0441\u043b\u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0430 MRC \u0431\u043b\u043e\u043a\u0430\u043c \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u0441\u043b\u0430\u0442\u044c copy.  <\/p>\n<pre><code class=\"objectivec\">- (void)someMethod {     return [^() {         ...     } copy]; } <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u0437\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u043d\u0430 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b __main_block_impl_0, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u0447\u0435\u043c \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043f\u043e\u043b\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0431\u043b\u043e\u043a\u0430 \u043f\u0440\u0438 \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438.  <\/p>\n<pre><code class=\"objectivec\">  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, NSString *_string, __Block_byref_blockString_0 *_blockString, int flags=0) : string(_string), blockString(_blockString-&gt;__forwarding) {     impl.isa = &_NSConcreteStackBlock;     impl.Flags = flags;     impl.FuncPtr = fp;     Desc = desc;   } <\/code><\/pre>\n<p>  \u0422.\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u0448 \u0431\u043b\u043e\u043a \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u0442\u0438\u043f\u0430 _NSConcreteStackBlock.<br \/>  FuncPtr \u2014 \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 C \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u044f\u0432\u043b\u044f\u044e\u0449\u0443\u044e\u0441\u044f \u0442\u0435\u043b\u043e\u043c \u0431\u043b\u043e\u043a\u0430<br \/>  flags \u2014 \u0440\u0430\u0432\u043d\u043e \u043d\u0443\u043b\u044e<br \/>  desc \u2014 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b __main_block_impl_0 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u043b\u043e\u043a\u043e\u0432<\/p>\n<h4>\u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435<\/h4>\n<p>  \u041f\u043e\u043c\u0438\u043c\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430 \u043f\u043e\u043b\u0435\u0439, \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 __main_block_impl_0 \u0445\u0440\u0430\u043d\u0438\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u044b\u0435 \u0431\u043b\u043e\u043a\u043e\u043c  <\/p>\n<pre><code class=\"cpp\">  NSString *string;   __Block_byref_blockString_0 *blockString;  <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f, \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u0430\u044f \u0441 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c __block \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 __Block_byref_blockString_0  <\/p>\n<pre><code class=\"cpp\">struct __Block_byref_blockString_0 {   void *__isa; __Block_byref_blockString_0 *__forwarding;  int __flags;  int __size;  void (*__Block_byref_id_object_copy)(void*, void*);  void (*__Block_byref_id_object_dispose)(void*);  NSString *blockString; }; <\/code><\/pre>\n<p>  \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cpp\">__attribute__((__blocks__(byref))) __Block_byref_blockString_0 blockString = {(void*)0,(__Block_byref_blockString_0 *)&blockString, 33554432, sizeof(__Block_byref_blockString_0), __Block_byref_id_object_copy_131, __Block_byref_id_object_dispose_131}; ; <\/code><\/pre>\n<p>  \u0422.\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u043f\u043e\u043b\u0435 blockString. \u0410 \u043f\u043e\u043b\u0435 __forwarding \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u0430\u043c\u043e \u043d\u0430 \u0441\u0435\u0431\u044f.<br \/>  \u041a\u0441\u0442\u0430\u0442\u0438 \u0432 __main_block_impl_0 \u043f\u043e\u043b\u0435 blockString \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c_blockString-&gt;__forwarding.<br \/>  \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u043b\u0435 __forwarding \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u043b\u043e\u043a\u043e\u0432 \u043c\u043e\u0433\u043b\u0438 \u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u0435\u043a\u0435 \u0438\u043b\u0438 \u0432 \u043a\u0443\u0447\u0435.<\/p>\n<h4>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u0418\u0442\u0430\u043a, \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u0431\u043b\u043e\u043a \u2014 \u044d\u0442\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043e\u0441\u043e\u0431\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 (NSMallocBlock, NSGlobalBlock, NSStackBlock) \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u0441\u044b\u043b\u043a\u0443 (\u043f\u043e\u043b\u0435 FuncPtr) \u043d\u0430 C \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u044f\u0432\u043b\u044f\u044e\u0449\u0443\u044e\u0441\u044f \u0442\u0435\u043b\u043e\u043c \u0431\u043b\u043e\u043a\u0430, \u0438, \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u044b\u0435 \u0431\u043b\u043e\u043a\u043e\u043c.<\/p>\n<p>  \u0422\u043e, \u0447\u0442\u043e \u0431\u043b\u043e\u043a \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u043c, \u0437\u043d\u0430\u0447\u0438\u0442 \u0442\u043e, \u0447\u0442\u043e \u0443 \u043d\u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0432\u0441\u044f\u043a\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b (\u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438). \u0421\u043f\u0438\u0441\u043e\u043a \u0443\u0436\u0435 \u0438\u043c\u0435\u044e\u0449\u0438\u0445\u0441\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0430\u0442\u044c <a href=\"https:\/\/github.com\/nst\/iOS-Runtime-Headers\/blob\/d576a9cc197412e81aa87624b755e59b4f00e3cd\/Frameworks\/CoreFoundation.framework\/\">\u0442\u0443\u0442<\/a>.<\/p>\n<p>  \u0412\u0441\u0435 \u0431\u043b\u043e\u043a\u0438 (Global, Stack, Malloc) \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442\u0441\u044f \u043e\u0442 \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e NSBlock, \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"objectivec\">@interface NSBlock : NSObject &lt;NSCopying&gt; { }  - (id)copy; - (id)copyWithZone:(struct _NSZone { }*)arg1; - (void)invoke; - (void)performAfterDelay:(double)arg1;  @end <\/code><\/pre>\n<p>  \u0422\u0430\u043a \u043a\u0430\u043a \u0431\u043b\u043e\u043a \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u043c NSObject, \u0442\u043e \u0443 \u043d\u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0432\u0441\u0435\u043c\u0438 \u043b\u044e\u0431\u0438\u043c\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 description, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u043f\u043e\u043d\u044f\u0442\u044c, \u0433\u0434\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432\u0430\u0448 \u0431\u043b\u043e\u043a. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440,  <\/p>\n<pre><code class=\"objectivec\">NSLog(@&quot;%@&quot;, [^{} description]); <\/code><\/pre>\n<p>  \u0432\u044b\u0432\u0435\u0434\u0435\u0442 &lt;__NSGlobalBlock__: 0x1000010c0&gt;. \u0427\u0442\u043e \u043d\u0430\u043c \u043d\u0430\u043c\u0435\u043a\u0430\u0435\u0442, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 \u043d\u0430\u043c\u0438 \u0431\u043b\u043e\u043a \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.<br \/>  \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u044b description \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 class.<\/p>\n<h4>\u0411\u043e\u043d\u0443\u0441<\/h4>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0448\u0443\u0442\u043a\u0438 \u0440\u0430\u0434\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0431\u043b\u043e\u043a\u0443 \u043c\u0435\u0442\u043e\u0434 repeat.<\/p>\n<pre><code class=\"objectivec\">#import &lt;Foundation\/Foundation.h&gt;  @interface NSBlock : NSObject &lt;NSCopying&gt;  - (void)invoke;  @end  @interface NSBlock (Ext)  - (void)repeat:(NSUInteger)count;  @end  @implementation NSBlock (Ext)  - (void)repeat:(NSUInteger)count {     for (int i = 0; i &lt; count; i++) {         [(NSBlock *)self invoke];     } }  @end  int main(int argc, const char * argv[]) {     [^{         NSLog(@&quot;Hello&quot;);     } repeat:3];          return 0; } <\/code><\/pre>\n<p>  <\/p>\n<h4>\u041f\u043e\u0441\u0442\u0441\u043a\u0440\u0438\u043f\u0442\u0443\u043c<\/h4>\n<p>  \u042f \u0441\u043e\u0437\u043d\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u043f\u0443\u0441\u0442\u0438\u043b \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0434\u0435\u0442\u0430\u043b\u044f\u0445, \u043a\u0430\u0441\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u044c\u044e (\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u043e\u0432, &#8230;).<br \/>  \u0415\u0441\u043b\u0438 \u043a\u043e\u043c\u0443-\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u0442\u043e \u043f\u0438\u0448\u0438\u0442\u0435. \u041f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043e\u0441\u0432\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0442\u0435\u043c\u0443.      \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/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\/company\/e-Legion\/blog\/224363\/\"> http:\/\/habrahabr.ru\/company\/e-Legion\/blog\/224363\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">     \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/736\/611\/3e8\/7366113e881ae5be22641494e257b2ea.jpg\" width=\"600\" alt=\"image\"\/><br \/>  \u0412 Objective-C \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u0430\u044f \u0448\u0442\u0443\u043a\u0430, \u043a\u0430\u043a \u0431\u043b\u043e\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438 <a href=\"http:\/\/ru.wikipedia.org\/wiki\/%D0%97%D0%B0%D0%BC%D1%8B%D0%BA%D0%B0%D0%BD%D0%B8%D0%B5_(%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5)\">\u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0439<\/a>.<\/p>\n<p>  \u0415\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0435\u0439 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u043b\u043e\u043a\u0438 (\u043a\u043e\u0433\u0434\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c copy, \u043a\u0430\u043a \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 retain \u0446\u0438\u043a\u043b\u043e\u0432 \u0438 \u0442. \u0434.), \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0431\u043b\u043e\u043a\u043e\u0432 \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435 \u0437\u0430\u0442\u0440\u0430\u0433\u0438\u0432\u0430\u044e\u0442. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u043d\u0438\u043c \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u0431\u0435\u043b.  <\/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-224363","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/224363","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=224363"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/224363\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=224363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=224363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=224363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}