{"id":38441,"date":"2025-05-20T13:02:37","date_gmt":"2025-05-20T05:02:37","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/38441.html"},"modified":"2025-05-20T13:02:37","modified_gmt":"2025-05-20T05:02:37","slug":"objective-c-block-%e5%ba%95%e5%b1%82%e5%8e%9f%e7%90%86%e6%b7%b1%e5%ba%a6%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/38441.html","title":{"rendered":"Objective-C Block \u5e95\u5c42\u539f\u7406\u6df1\u5ea6\u89e3\u6790"},"content":{"rendered":"<h2>Objective-C Block \u5e95\u5c42\u539f\u7406\u6df1\u5ea6\u89e3\u6790<\/h2>\n<h3>1. Block \u662f\u4ec0\u4e48&#xff1f;<\/h3>\n<h4>1.1 Block \u7684\u672c\u8d28<\/h4>\n<ul>\n<li>Block \u662f Objective-C \u4e2d\u7684\u7279\u6b8a\u5bf9\u8c61&#xff0c;\u5b9e\u73b0\u4e86\u533f\u540d\u51fd\u6570\u7684\u529f\u80fd\n<ul>\n<li>\u901a\u8fc7 isa \u6307\u9488\u7ee7\u627f\u81ea NSObject&#xff0c;\u53ef\u4ee5\u54cd\u5e94&#xff08;\u5982 copy\u3001retain\u3001release&#xff09;\u7b49\u5185\u5b58\u7ba1\u7406\u65b9\u6cd5&#xff08;ARC \u4e0b\u81ea\u52a8\u5904\u7406&#xff09;\u3002<\/li>\n<li>\u9075\u5faa Objective-C \u7684\u5185\u5b58\u7ba1\u7406\u89c4\u5219<\/li>\n<li>\u53ef\u4ee5\u4e34\u65f6\u4fdd\u5b58\u4ee3\u7801\u5e76\u5728\u9700\u8981\u65f6\u6267\u884c<\/li>\n<li>\u53ef\u4ee5\u8bbf\u95ee\u5b9a\u4e49\u65f6\u7684\u4e0a\u4e0b\u6587\u53d8\u91cf<\/li>\n<li>\u53ef\u4ee5\u50cf\u666e\u901a\u5bf9\u8c61\u4e00\u6837\u4f20\u9012\u548c\u4f7f\u7528<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>1.2 Block \u7684\u57fa\u672c\u5199\u6cd5<\/h4>\n<p>\/\/ \u6700\u7b80\u5355\u7684 Block<br \/>\nvoid (^sayHello)(void) &#061; ^{<br \/>\n    NSLog(&#064;&#034;Hello&#034;);<br \/>\n};<\/p>\n<p>\/\/ \u5e26\u53c2\u6570\u7684 Block<br \/>\nvoid (^saySomething)(NSString *) &#061; ^(NSString *words) {<br \/>\n    NSLog(&#064;&#034;%&#064;&#034;, words);<br \/>\n};<\/p>\n<p>\/\/ \u5e26\u8fd4\u56de\u503c\u7684 Block<br \/>\nint (^add)(int, int) &#061; ^(int a, int b) {<br \/>\n    return a &#043; b;<br \/>\n};<\/p>\n<h4>1.3 Block \u7684\u5e95\u5c42\u7ed3\u6784<\/h4>\n<p><span class=\"token comment\">\/\/ Block \u7684\u672c\u7ed3\u6784<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">Block_layout<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span>isa<span class=\"token punctuation\">;<\/span>              <span class=\"token comment\">\/\/ \u6307\u5411\u7c7b\u5bf9\u8c61&#xff0c;\u8bc1\u660e Block \u662f\u5bf9\u8c61<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> flags<span class=\"token punctuation\">;<\/span>              <span class=\"token comment\">\/\/ \u72b6\u6001\u6807\u5fd7&#xff0c;\u5305\u542b Block \u7684\u7c7b\u578b\u548c\u5f15\u7528\u8ba1\u6570\u4fe1\u606f<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> reserved<span class=\"token punctuation\">;<\/span>           <span class=\"token comment\">\/\/ \u4fdd\u7559\u5b57\u6bb5<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>invoke<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u51fd\u6570\u6307\u9488&#xff0c;\u6307\u5411 Block \u7684\u5b9e\u73b0\u4ee3\u7801<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">Block_descriptor<\/span> <span class=\"token operator\">*<\/span>descriptor<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u63cf\u8ff0\u4fe1\u606f&#xff0c;\u5305\u542b Block \u7684\u5927\u5c0f\u3001\u590d\u5236\u548c\u91ca\u653e\u51fd\u6570\u7b49<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u6355\u83b7\u7684\u53d8\u91cf<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u7ed3\u6784\u89e3\u6790&#xff1a;<\/p>\n<ul>\n<li>isa \u6307\u9488&#xff1a;\u8868\u660e Block \u662f\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u6307\u5411\u5176\u7c7b\u578b\u4fe1\u606f&#xff08;_NSConcreteGlobalBlock\u3001_NSConcreteStackBlock \u6216 _NSConcreteMallocBlock&#xff09;<\/li>\n<li>Flags&#xff1a;\u8bb0\u5f55 Block \u7684\u72b6\u6001\u4fe1\u606f&#xff0c;\u5982\u662f\u5426\u88ab\u590d\u5236\u5230\u5806\u4e0a\u3001\u662f\u5426\u5305\u542b C&#043;&#043; \u5bf9\u8c61\u7b49<\/li>\n<li>reserved&#xff1a;\u4fdd\u7559\u5b57\u6bb5&#xff0c;\u7528\u4e8e\u672a\u6765\u6269\u5c55<\/li>\n<li>invoke&#xff1a;\u6307\u5411 Block \u8981\u6267\u884c\u7684\u4ee3\u7801<\/li>\n<li>descriptor&#xff1a;\u5305\u542b Block \u7684\u63cf\u8ff0\u4fe1\u606f&#xff0c;\u5982\u5927\u5c0f\u3001\u590d\u5236\u548c\u91ca\u653e\u51fd\u6570\u7b49<\/li>\n<li>\u8fd9\u4e2a\u7ed3\u6784\u4f53\u662f Block \u4f5c\u4e3a\u5bf9\u8c61\u548c\u51fd\u6570\u53cc\u91cd\u8eab\u4efd\u7684\u57fa\u7840<\/li>\n<\/ul>\n<p>\u8865\u5145\u8bf4\u660e&#xff1a;<\/p>\n<ul>\n<li>Block \u662f\u7279\u6b8a\u7684 Objective-C \u5bf9\u8c61&#xff0c;\u53ef\u4ee5\u54cd\u5e94 copy\u3001release \u7b49\u6d88\u606f<\/li>\n<li>\u5185\u5b58\u7ba1\u7406\u9075\u5faa Objective-C \u7684\u5185\u5b58\u7ba1\u7406\u89c4\u5219<\/li>\n<li>\u5728\u7f16\u8bd1\u65f6\u4f1a\u6839\u636e\u4f7f\u7528\u573a\u666f\u751f\u6210\u4e0d\u540c\u7684\u7c7b\u578b&#xff08;\u5168\u5c40\u3001\u6808\u3001\u5806&#xff09;<\/li>\n<li>\u7ed3\u6784\u4f53\u4e2d\u7684 isa \u6307\u9488\u51b3\u5b9a\u4e86 Block \u7684\u7c7b\u578b\u548c\u884c\u4e3a<\/li>\n<\/ul>\n<h4>1.4 Block \u7684\u7c7b\u578b\u7f16\u7801<\/h4>\n<p>\/\/ \u57fa\u672c\u683c\u5f0f&#xff1a;&#064;?&lt;\u8fd4\u56de\u503c\u7c7b\u578b\u53c2\u65701\u7c7b\u578b\u53c2\u65702\u7c7b\u578b&#8230;&gt;<br \/>\n\/\/ \u793a\u4f8b1&#xff1a;\u65e0\u53c2\u6570\u65e0\u8fd4\u56de\u503c\u7684 Block<br \/>\n&#034;&#064;?&lt;v&gt;&#034;  \/\/ void (^)(void)<\/p>\n<p>\/\/ \u793a\u4f8b2&#xff1a;\u5e26\u57fa\u672c\u7c7b\u578b\u53c2\u6570\u7684 Block<br \/>\n&#034;&#064;?&lt;vi&gt;&#034;  \/\/ void (^)(int)<br \/>\n\/\/ v \u8868\u793a\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void<br \/>\n\/\/ i \u8868\u793a\u53c2\u6570\u7c7b\u578b\u4e3a int (\u6ce8\u610f&#xff1a;\u57fa\u672c\u7c7b\u578b\u4e0d\u9700\u8981&#064;\u524d\u7f00)<\/p>\n<p>\/\/ \u793a\u4f8b3&#xff1a;\u5e26\u5bf9\u8c61\u53c2\u6570\u7684 Block<br \/>\n&#034;&#064;?&lt;v&#064;&gt;&#034;   \/\/ void (^)(id)<br \/>\n\/\/ v \u8868\u793a\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void<br \/>\n\/\/ &#064; \u8868\u793a\u53c2\u6570\u7c7b\u578b\u4e3a id&#xff08;\u5bf9\u8c61\u7c7b\u578b&#xff09;<\/p>\n<p>\/\/ \u793a\u4f8b4&#xff1a;\u5e26 Block \u53c2\u6570\u7684 Block<br \/>\n&#034;&#064;?&lt;v&#064;?&#034;  \/\/ void (^)(void (^)())<br \/>\n\/\/ v \u8868\u793a\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void<br \/>\n\/\/ &#064;? \u8868\u793a\u53c2\u6570\u7c7b\u578b\u4e3a Block<\/p>\n<p>\/\/ \u793a\u4f8b5&#xff1a;\u5e26\u8fd4\u56de\u503c\u7684 Block<br \/>\n&#034;&#064;?&lt;i&#064;&gt;&#034;   \/\/ int (^)(id)<br \/>\n\/\/ i \u8868\u793a\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a int (\u57fa\u672c\u7c7b\u578b\u4e0d\u9700\u8981&#064;\u524d\u7f00)<br \/>\n\/\/ &#064; \u8868\u793a\u53c2\u6570\u7c7b\u578b\u4e3a id<\/p>\n<p>\/\/ \u793a\u4f8b6&#xff1a;\u5e26\u591a\u4e2a\u53c2\u6570\u7684 Block<br \/>\n&#034;&#064;?&lt;viB&#064;&gt;&#034;  \/\/ void (^)(int, BOOL, id)<br \/>\n\/\/ v \u8868\u793a\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a void<br \/>\n\/\/ i \u8868\u793a\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a int<br \/>\n\/\/ B \u8868\u793a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a BOOL<br \/>\n\/\/ &#064; \u8868\u793a\u7b2c\u4e09\u4e2a\u53c2\u6570\u4e3a id<\/p>\n<h5>\u7c7b\u578b\u7f16\u7801\u89c4\u5219&#xff1a;<\/h5>\n<p>\u57fa\u672c\u7c7b\u578b&#xff08;int\u3001BOOL\u7b49&#xff09;\u4e0d\u9700\u8981\u52a0&#064;\u524d\u7f00 \u8fd4\u56de\u503c\u7c7b\u578b\u5982\u679c\u662f\u5bf9\u8c61&#xff0c;\u9700\u8981\u52a0&#064; \u53c2\u6570\u5217\u8868\u4e2d\u7684\u7c7b\u578b\u6309\u987a\u5e8f\u6392\u5217&#xff0c;\u4e0d\u9700\u8981\u5206\u9694\u7b26 \u5bf9\u4e8e\u5177\u4f53\u7c7b\u540d\u7684\u5bf9\u8c61&#xff0c;\u53ef\u4ee5\u4f7f\u7528&#064;&#034;ClassName&#034;\u683c\u5f0f<\/p>\n<ul>\n<li>&#064;?&#xff1a;\u8868\u793a\u8fd9\u662f\u4e00\u4e2a Block \u7c7b\u578b<\/li>\n<li>&lt; &gt;&#xff1a;\u5305\u542b Block \u7684\u7b7e\u540d\u4fe1\u606f\n<ul>\n<li>\u683c\u5f0f&#xff1a;&lt;\u8fd4\u56de\u503c\u7c7b\u578b\u53c2\u65701\u7c7b\u578b\u53c2\u65702\u7c7b\u578b&#8230;&gt;<\/li>\n<li>\u8fd4\u56de\u503c\u7c7b\u578b\u5728\u524d&#xff0c;\u53c2\u6570\u7c7b\u578b\u4f9d\u6b21\u6392\u5217<\/li>\n<\/ul>\n<\/li>\n<li>\u7c7b\u578b\u8868\u793a\n<ul>\n<li>\u57fa\u672c\u7c7b\u578b&#xff1a;\n<ul>\n<li>v&#xff1a;void<\/li>\n<li>i&#xff1a;int<\/li>\n<li>c&#xff1a;char<\/li>\n<li>B&#xff1a;BOOL<\/li>\n<li>f&#xff1a;float<\/li>\n<li>d&#xff1a;double<\/li>\n<li>q&#xff1a;NSInteger<\/li>\n<li>Q&#xff1a;NSUInteger<\/li>\n<\/ul>\n<\/li>\n<li>\u5bf9\u8c61\u7c7b\u578b&#xff1a;\n<ul>\n<li>&#064;: id\/NSObject<\/li>\n<li>&#064;\u201cNSString\u201d: NSString* (\u5177\u4f53\u7c7b\u540d)<\/li>\n<\/ul>\n<\/li>\n<li>Block\u7c7b\u578b&#xff1a;\n<ul>\n<li>&#064;?: \u53e6\u4e00\u4e2aBlock<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\/\/ \u590d\u6742\u793a\u4f8b<br \/>\ntypedef NSString *(^ComplexBlock)(int, void (^)(BOOL), NSArray&lt;NSNumber *&gt; *);<\/p>\n<p>\/\/ \u5bf9\u5e94\u7684\u7c7b\u578b\u7f16\u7801<br \/>\n&#034;&#064;?&lt;&#064;i&#064;?&#064;&gt;&#034;<br \/>\n\/\/ \u6216\u66f4\u7cbe\u786e\u7684&#xff08;\u5982\u679c\u5e0c\u671b\u5305\u542b\u7c7b\u540d&#xff09;&#xff1a;<br \/>\n&#034;&#064;?&lt;&#064;&#034;NSString&#034;i&#064;?&#064;&#034;NSArray&#034;&gt;&#034;<\/p>\n<p>\u9010\u90e8\u5206\u89e3\u6790&#xff1a;<\/p>\n<table>\n<tr>\u90e8\u5206\u7f16\u7801\u5bf9\u5e94\u7c7b\u578b<\/tr>\n<tbody>\n<tr>\n<td>\u8fd4\u56de\u503c<\/td>\n<td>&#064; \u6216 &#064;\u201cNSString\u201d<\/td>\n<td>NSString *<\/td>\n<\/tr>\n<tr>\n<td>\u53c2\u65701<\/td>\n<td>i<\/td>\n<td>int&#xff08;\u57fa\u672c\u7c7b\u578b&#xff0c;\u4e0d\u52a0 &#064;&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u53c2\u65702<\/td>\n<td>&#064;?<\/td>\n<td>void (^)(BOOL)&#xff08;Block \u53c2\u6570&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u53c2\u65703<\/td>\n<td>&#064; \u6216 &#064;\u201cNSArray\u201d<\/td>\n<td>NSArray *&#xff08;\u5bf9\u8c61\u7c7b\u578b&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>2. Block \u7684\u4e09\u79cd\u7c7b\u578b<\/h3>\n<h4>2.1 \u5168\u5c40 Block&#xff08;Global Block&#xff09;<\/h4>\n<p>\/\/ \u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df<br \/>\nvoid (^globalBlock)(void) &#061; ^{<br \/>\n    NSLog(&#064;&#034;\u6211\u662f\u5168\u5c40 Block&#034;);<br \/>\n};<\/p>\n<p>\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u4e0d\u8bbf\u95ee\u4efb\u4f55\u5916\u90e8\u53d8\u91cf&#xff0c;\u5b8c\u5168\u72ec\u7acb\u4e8e\u4e0a\u4e0b\u6587<\/li>\n<li>\u4f5c\u4e3a\u5355\u4f8b\u5bf9\u8c61\u5b58\u50a8\u5728\u7a0b\u5e8f\u7684\u6570\u636e\u533a&#xff0c;\u751f\u547d\u5468\u671f\u4e0e\u7a0b\u5e8f\u76f8\u540c<\/li>\n<li>\u4e0d\u9700\u8981\u7279\u522b\u7684\u5185\u5b58\u7ba1\u7406&#xff0c;\u7531\u7cfb\u7edf\u81ea\u52a8\u7ba1\u7406<\/li>\n<li>\u6027\u80fd\u6700\u4f18&#xff0c;\u56e0\u4e3a\u4e0d\u9700\u8981\u6355\u83b7\u53d8\u91cf\u548c\u5185\u5b58\u7ba1\u7406\u5f00\u9500<\/li>\n<\/ul>\n<h4>2.2 \u6808 Block&#xff08;Stack Block&#xff09;<\/h4>\n<p>&#8211; (void)example {<br \/>\n    int num &#061; 10;<br \/>\n    void (^stackBlock)(void) &#061; ^{<br \/>\n        NSLog(&#064;&#034;\u6211\u662f\u6808 Block: %d&#034;, num);<br \/>\n    };<br \/>\n}<\/p>\n<p>\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u8bbf\u95ee\u5916\u90e8\u53d8\u91cf&#xff0c;\u9700\u8981\u6355\u83b7\u4e0a\u4e0b\u6587\u4e2d\u7684\u53d8\u91cf<\/li>\n<li>\u4f5c\u4e3a\u4e34\u65f6\u5bf9\u8c61\u5b58\u50a8\u5728\u6808\u4e0a&#xff0c;\u751f\u547d\u5468\u671f\u4e0e\u6240\u5728\u51fd\u6570\u76f8\u540c<\/li>\n<li>\u51fd\u6570\u6267\u884c\u7ed3\u675f\u540e\u53ef\u80fd\u5931\u6548&#xff0c;\u9700\u8981\u590d\u5236\u5230\u5806\u4e0a\u624d\u80fd\u957f\u671f\u4f7f\u7528<\/li>\n<li>\u6027\u80fd\u8f83\u597d&#xff0c;\u4f46\u9700\u8981\u6ce8\u610f\u4f7f\u7528\u65f6\u673a\u548c\u751f\u547d\u5468\u671f<\/li>\n<\/ul>\n<h4>2.3 \u5806 Block&#xff08;Malloc Block&#xff09;<\/h4>\n<p>&#8211; (void)example {<br \/>\n    int num &#061; 10;<br \/>\n    \/\/ \u65b9\u5f0f1&#xff1a;\u624b\u52a8\u590d\u5236\u5230\u5806<br \/>\n    void (^heapBlock1)(void) &#061; [^{<br \/>\n        NSLog(&#064;&#034;\u6211\u662f\u5806 Block: %d&#034;, num);<br \/>\n    } copy];<\/p>\n<p>    \/\/ \u65b9\u5f0f2&#xff1a;\u8d4b\u503c\u7ed9\u5c5e\u6027\u81ea\u52a8\u590d\u5236\u5230\u5806<br \/>\n    self.block &#061; ^{<br \/>\n        NSLog(&#064;&#034;\u6211\u662f\u5806 Block: %d&#034;, num);<br \/>\n    };<br \/>\n}<\/p>\n<p>\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u7531\u6808 Block \u590d\u5236\u800c\u6765&#xff0c;\u5b58\u50a8\u5728\u5806\u4e0a&#xff0c;\u53ef\u4ee5\u957f\u671f\u4f7f\u7528<\/li>\n<li>\u9700\u8981\u7ba1\u7406\u5185\u5b58&#xff0c;\u5728 ARC \u73af\u5883\u4e0b\u7531\u7cfb\u7edf\u81ea\u52a8\u7ba1\u7406<\/li>\n<li>\u53ef\u4ee5\u5b89\u5168\u5730\u8de8\u51fd\u6570\u4f20\u9012\u548c\u4f7f\u7528<\/li>\n<li>\u6027\u80fd\u76f8\u5bf9\u8f83\u4f4e&#xff0c;\u4f46\u63d0\u4f9b\u4e86\u6700\u5927\u7684\u7075\u6d3b\u6027\u548c\u5b89\u5168\u6027<\/li>\n<\/ul>\n<h4>2.4 \u5185\u5b58\u7ba1\u7406\u5dee\u5f02<\/h4>\n<h4>\u7c7b\u578b\u8f6c\u6362\u89c4\u5219&#xff08;ARC vs MRC&#xff09;<\/h4>\n<table>\n<tr>\u64cd\u4f5c\u573a\u666fARC \u73af\u5883MRC \u73af\u5883<\/tr>\n<tbody>\n<tr>\n<td>\u8d4b\u503c\u7ed9 strong \u53d8\u91cf<\/td>\n<td>\u81ea\u52a8 copy \u5230\u5806<\/td>\n<td>\u9700\u8981\u624b\u52a8\u8c03\u7528 copy<\/td>\n<\/tr>\n<tr>\n<td>\u4f5c\u4e3a\u51fd\u6570\u8fd4\u56de\u503c<\/td>\n<td>\u81ea\u52a8 copy \u5230\u5806<\/td>\n<td>\u9700\u8981\u624b\u52a8\u8c03\u7528 copy<\/td>\n<\/tr>\n<tr>\n<td>\u4f20\u9012\u7ed9 GCD API<\/td>\n<td>\u81ea\u52a8 copy \u5230\u5806<\/td>\n<td>\u81ea\u52a8 copy \u5230\u5806<\/td>\n<\/tr>\n<tr>\n<td>\u4f5c\u4e3a\u5c5e\u6027(copy)<\/td>\n<td>\u81ea\u52a8\u5904\u7406<\/td>\n<td>\u5fc5\u987b\u663e\u5f0f copy<\/td>\n<\/tr>\n<tr>\n<td>\u4f5c\u4e3a\u5c40\u90e8\u53d8\u91cf<\/td>\n<td>\u4fdd\u6301\u5728\u6808\u4e0a<\/td>\n<td>\u4fdd\u6301\u5728\u6808\u4e0a<\/td>\n<\/tr>\n<tr>\n<td>\u4f5c\u4e3a\u5168\u5c40\u53d8\u91cf<\/td>\n<td>\u4fdd\u6301\u5728\u6570\u636e\u533a<\/td>\n<td>\u4fdd\u6301\u5728\u6570\u636e\u533a<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>3. Block \u5982\u4f55\u6355\u83b7\u53d8\u91cf<\/h3>\n<h4>3.1 \u57fa\u672c\u7c7b\u578b\u53d8\u91cf<\/h4>\n<p>int num &#061; 10;<br \/>\nvoid (^block)(void) &#061; ^{<br \/>\n    NSLog(&#064;&#034;%d&#034;, num);  \/\/ \u53ea\u80fd\u8bfb\u53d6&#xff0c;\u4e0d\u80fd\u4fee\u6539<br \/>\n};<\/p>\n<p>\u7f16\u8bd1\u540e\u7684\u7ed3\u6784&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ \u7f16\u8bd1\u5668\u751f\u6210\u7684 Block \u7ed3\u6784\u4f53<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__main_block_impl_0<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__block_impl<\/span> impl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__main_block_desc_0<\/span><span class=\"token operator\">*<\/span> Desc<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> num<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u6355\u83b7\u7684\u53d8\u91cf<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4f7f\u7528\u793a\u4f8b<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__main_block_impl_0<\/span> block <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token punctuation\">.<\/span>impl <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token punctuation\">.<\/span>Desc <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token punctuation\">.<\/span>num <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">10<\/span>  <span class=\"token comment\">\/\/ \u503c\u590d\u5236<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u5728\u7f16\u8bd1\u65f6&#xff0c;Block \u4f1a\u521b\u5efa\u4e00\u4e2a\u7ed3\u6784\u4f53\u6765\u5b58\u50a8\u6355\u83b7\u7684\u53d8\u91cf<\/li>\n<li>\u53d8\u91cf\u7684\u503c\u4f1a\u88ab\u590d\u5236\u5230 Block \u7684\u7ed3\u6784\u4f53\u4e2d&#xff0c;\u5f62\u6210\u72ec\u7acb\u7684\u526f\u672c<\/li>\n<li>Block \u5185\u90e8\u4f7f\u7528\u7684\u662f\u8fd9\u4e2a\u526f\u672c&#xff0c;\u4e0e\u5916\u90e8\u53d8\u91cf\u5b8c\u5168\u72ec\u7acb<\/li>\n<li>\u8fd9\u79cd\u503c\u590d\u5236\u673a\u5236\u4fdd\u8bc1\u4e86 Block \u6267\u884c\u65f6\u6570\u636e\u7684\u7a33\u5b9a\u6027<\/li>\n<\/ul>\n<h4>3.2 \u4f7f\u7528 __block \u4fee\u9970\u7b26<\/h4>\n<p>__block int num &#061; 10;<br \/>\nvoid (^block)(void) &#061; ^{<br \/>\n    num &#061; 20;  \/\/ \u53ef\u4ee5\u4fee\u6539<br \/>\n    NSLog(&#064;&#034;%d&#034;, num);<br \/>\n};<\/p>\n<p>\u7f16\u8bd1\u540e\u7684\u7ed3\u6784&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ __block \u53d8\u91cf\u7684\u5305\u88c5\u7ed3\u6784\u4f53<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__Block_byref_num_0<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span>__isa<span class=\"token punctuation\">;<\/span><br \/>\n    __Block_byref_num_0 <span class=\"token operator\">*<\/span>__forwarding<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> __flags<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> __size<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> num<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u539f\u59cb\u53d8\u91cf<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ Block \u7ed3\u6784\u4f53<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__main_block_impl_0<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__block_impl<\/span> impl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__main_block_desc_0<\/span><span class=\"token operator\">*<\/span> Desc<span class=\"token punctuation\">;<\/span><br \/>\n    __Block_byref_num_0 <span class=\"token operator\">*<\/span>num<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u901a\u8fc7\u6307\u9488\u5f15\u7528<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>__block \u4fee\u9970\u7b26\u4f1a\u5c06\u88ab\u4fee\u9970\u7684\u53d8\u91cf\u5305\u88c5\u6210\u4e00\u4e2a\u7ed3\u6784\u4f53<\/li>\n<li>\u8fd9\u4e2a\u7ed3\u6784\u4f53\u5305\u542b\u539f\u59cb\u53d8\u91cf\u7684\u6307\u9488&#xff0c;\u4f7f\u5f97 Block \u53ef\u4ee5\u4fee\u6539\u539f\u59cb\u53d8\u91cf<\/li>\n<li>\u5728 Block \u88ab\u590d\u5236\u5230\u5806\u4e0a\u65f6&#xff0c;\u8fd9\u4e2a\u7ed3\u6784\u4f53\u4e5f\u4f1a\u88ab\u590d\u5236<\/li>\n<li>\u6240\u6709\u5f15\u7528\u8fd9\u4e2a\u53d8\u91cf\u7684 Block \u5171\u4eab\u540c\u4e00\u4e2a\u7ed3\u6784\u4f53&#xff0c;\u4fdd\u8bc1\u4fee\u6539\u7684\u540c\u6b65\u6027<\/li>\n<\/ul>\n<h4>3.3 \u5bf9\u8c61\u7c7b\u578b\u53d8\u91cf<\/h4>\n<p>NSObject *obj &#061; [[NSObject alloc] init];<br \/>\nvoid (^block)(void) &#061; ^{<br \/>\n    NSLog(&#064;&#034;%&#064;&#034;, obj);  \/\/ \u5f3a\u5f15\u7528<br \/>\n};<\/p>\n<p>\u7f16\u8bd1\u540e\u7684\u7ed3\u6784&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ Block \u7ed3\u6784\u4f53<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__main_block_impl_0<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__block_impl<\/span> impl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__main_block_desc_0<\/span><span class=\"token operator\">*<\/span> Desc<span class=\"token punctuation\">;<\/span><br \/>\n    NSObject <span class=\"token operator\">*<\/span>__strong obj<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u5f3a\u5f15\u7528<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u7f16\u8bd1\u5668\u751f\u6210\u7684\u8f85\u52a9\u51fd\u6570<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>copy<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__main_block_impl_0<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__main_block_impl_0<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;<\/span> __main_block_copy_0<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>dispose<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">__main_block_impl_0<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;<\/span> __main_block_dispose_0<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u5bf9\u8c61\u7c7b\u578b\u7684\u53d8\u91cf\u5728 Block \u4e2d\u9ed8\u8ba4\u662f\u5f3a\u5f15\u7528<\/li>\n<li>Block \u4f1a\u901a\u8fc7 retain \u64cd\u4f5c\u589e\u52a0\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570<\/li>\n<li>\u5f53 Block \u88ab\u590d\u5236\u5230\u5806\u4e0a\u65f6&#xff0c;\u4f1a\u518d\u6b21 retain \u5bf9\u8c61<\/li>\n<li>\u9700\u8981\u6ce8\u610f\u5faa\u73af\u5f15\u7528\u95ee\u9898&#xff0c;\u53ef\u4ee5\u4f7f\u7528 __weak \u4fee\u9970\u7b26\u6765\u907f\u514d<\/li>\n<li>\u5728 ARC \u73af\u5883\u4e0b&#xff0c;Block \u4f1a\u81ea\u52a8\u7ba1\u7406\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570<\/li>\n<\/ul>\n<p>\u5e95\u5c42\u5b9e\u73b0\u539f\u7406&#xff1a;<\/p>\n<ul>\n<li>Block \u5728\u7f16\u8bd1\u65f6\u4f1a\u751f\u6210\u4e00\u4e2a\u7ed3\u6784\u4f53&#xff0c;\u5305\u542b\u6355\u83b7\u7684\u53d8\u91cf<\/li>\n<li>\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b&#xff0c;\u76f4\u63a5\u590d\u5236\u503c\u5230\u7ed3\u6784\u4f53\u4e2d<\/li>\n<li>\u5bf9\u4e8e __block \u53d8\u91cf&#xff0c;\u4f1a\u751f\u6210\u4e00\u4e2a\u5305\u88c5\u7ed3\u6784\u4f53<\/li>\n<li>\u5bf9\u4e8e\u5bf9\u8c61\u7c7b\u578b&#xff0c;\u4f1a\u751f\u6210\u76f8\u5e94\u7684\u5f15\u7528\u8ba1\u6570\u7ba1\u7406\u4ee3\u7801<\/li>\n<li>Block \u7684 copy \u64cd\u4f5c\u4f1a\u9012\u5f52\u590d\u5236\u6240\u6709\u6355\u83b7\u7684\u53d8\u91cf<\/li>\n<\/ul>\n<h4>3.4 \u5c0f\u7ed3<\/h4>\n<ul>\n<li>\u7f16\u8bd1\u65f6\u5904\u7406&#xff1a;Block \u5728\u7f16\u8bd1\u65f6\u4f1a\u751f\u6210\u5305\u542b\u6355\u83b7\u53d8\u91cf\u7684\u7ed3\u6784\u4f53&#xff0c;\u5e76\u6839\u636e\u53d8\u91cf\u7c7b\u578b\u751f\u6210\u76f8\u5e94\u7684\u5185\u5b58\u7ba1\u7406\u4ee3\u7801\u548c\u8f85\u52a9\u51fd\u6570\u3002<\/li>\n<li>\u53d8\u91cf\u6355\u83b7\u65b9\u5f0f&#xff1a;\u57fa\u672c\u7c7b\u578b\u901a\u8fc7\u503c\u590d\u5236\u5f62\u6210\u72ec\u7acb\u526f\u672c&#xff0c;__block \u53d8\u91cf\u901a\u8fc7\u5305\u88c5\u7ed3\u6784\u4f53\u5b9e\u73b0\u5171\u4eab\u8bbf\u95ee&#xff0c;\u5bf9\u8c61\u7c7b\u578b\u901a\u8fc7\u5f15\u7528\u8ba1\u6570\u7ba1\u7406\u5b9e\u73b0\u5185\u5b58\u7ba1\u7406\u3002<\/li>\n<li>\u5185\u5b58\u7ba1\u7406\u7279\u70b9&#xff1a;\u57fa\u672c\u7c7b\u578b\u65e0\u9700\u7279\u6b8a\u7ba1\u7406&#xff0c;__block \u53d8\u91cf\u901a\u8fc7\u5171\u4eab\u7ed3\u6784\u4f53\u5b9e\u73b0\u540c\u6b65\u4fee\u6539&#xff0c;\u5bf9\u8c61\u7c7b\u578b\u901a\u8fc7\u81ea\u52a8\u5f15\u7528\u8ba1\u6570\u7ba1\u7406\u751f\u547d\u5468\u671f\u3002<\/li>\n<li>\u6027\u80fd\u8003\u8651&#xff1a;\u57fa\u672c\u7c7b\u578b\u6027\u80fd\u6700\u4f18&#xff0c;__block \u53d8\u91cf\u6709\u6307\u9488\u95f4\u63a5\u8bbf\u95ee\u5f00\u9500&#xff0c;\u5bf9\u8c61\u7c7b\u578b\u6709\u5f15\u7528\u8ba1\u6570\u7ba1\u7406\u5f00\u9500\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>4. Block \u7684\u5185\u5b58\u7ba1\u7406\u4e0e\u5faa\u73af\u5f15\u7528<\/h3>\n<h4>4.1 \u5185\u5b58\u7ba1\u7406\u89c4\u5219<\/h4>\n<li>\u5168\u5c40 Block&#xff1a;\u4f5c\u4e3a\u5355\u4f8b\u5bf9\u8c61\u5b58\u50a8\u5728\u6570\u636e\u533a&#xff0c;\u751f\u547d\u5468\u671f\u4e0e\u7a0b\u5e8f\u76f8\u540c&#xff0c;\u65e0\u9700\u7279\u6b8a\u7ba1\u7406\u3002<\/li>\n<li>\u6808 Block&#xff1a;\u4f5c\u4e3a\u4e34\u65f6\u5bf9\u8c61\u5b58\u50a8\u5728\u6808\u4e0a&#xff0c;\u5728\u8d4b\u503c\u7ed9 strong \u5c5e\u6027\u3001\u4f5c\u4e3a\u8fd4\u56de\u503c\u6216\u4f20\u9012\u7ed9 GCD \u65f6\u4f1a\u81ea\u52a8\u590d\u5236\u5230\u5806\u4e0a\u3002<\/li>\n<li>\u5806 Block&#xff1a;\u7531\u6808 Block \u590d\u5236\u800c\u6765&#xff0c;\u5728 ARC \u73af\u5883\u4e0b\u81ea\u52a8\u7ba1\u7406\u5185\u5b58&#xff0c;\u9075\u5faa\u666e\u901a\u5bf9\u8c61\u7684\u5185\u5b58\u7ba1\u7406\u89c4\u5219\u3002<\/li>\n<h4>4.2 \u5faa\u73af\u5f15\u7528\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h4>\n<h5>\u5faa\u73af\u5f15\u7528\u793a\u4f8b<\/h5>\n<p>&#064;interface DownloadManager : NSObject<br \/>\n&#064;property (nonatomic, copy) void (^progressBlock)(CGFloat progress);<br \/>\n&#064;property (nonatomic, copy) void (^completionBlock)(void);<br \/>\n&#064;end<\/p>\n<p>&#064;interface ViewController : UIViewController<br \/>\n&#064;property (nonatomic, strong) DownloadManager *downloadManager;<br \/>\n&#064;end<\/p>\n<p>&#064;implementation ViewController<br \/>\n&#8211; (void)setup {<br \/>\n    \/\/ \u9519\u8bef\u5199\u6cd5&#xff1a;\u5f62\u6210\u5faa\u73af\u5f15\u7528<br \/>\n    self.downloadManager &#061; [[DownloadManager alloc] init];<\/p>\n<p>    \/\/ \u4e0b\u8f7d\u8fdb\u5ea6\u56de\u8c03<br \/>\n    self.downloadManager.progressBlock &#061; ^(CGFloat progress) {<br \/>\n        \/\/ ViewController \u6301\u6709 downloadManager<br \/>\n        \/\/ progressBlock \u6301\u6709 ViewController<br \/>\n        [self updateProgress:progress];  \/\/ \u76f4\u63a5\u4f7f\u7528 self \u5bfc\u81f4\u5faa\u73af\u5f15\u7528<br \/>\n    };<\/p>\n<p>    \/\/ \u4e0b\u8f7d\u5b8c\u6210\u56de\u8c03<br \/>\n    self.downloadManager.completionBlock &#061; ^{<br \/>\n        \/\/ ViewController \u6301\u6709 downloadManager<br \/>\n        \/\/ completionBlock \u6301\u6709 ViewController<br \/>\n        [self handleDownloadComplete];  \/\/ \u76f4\u63a5\u4f7f\u7528 self \u5bfc\u81f4\u5faa\u73af\u5f15\u7528<br \/>\n    };<br \/>\n}<\/p>\n<p>&#8211; (void)updateProgress:(CGFloat)progress {<br \/>\n    \/\/ \u66f4\u65b0\u8fdb\u5ea6\u6761<br \/>\n}<\/p>\n<p>&#8211; (void)handleDownloadComplete {<br \/>\n    \/\/ \u5904\u7406\u4e0b\u8f7d\u5b8c\u6210<br \/>\n}<br \/>\n&#064;end<\/p>\n<h5>\u5faa\u73af\u5f15\u7528\u5206\u6790&#xff1a;<\/h5>\n<li>ViewController \u6301\u6709 downloadManager&#xff08;\u5f3a\u5f15\u7528&#xff09;<\/li>\n<li>downloadManager \u6301\u6709 progressBlock \u548c completionBlock&#xff08;copy \u5c5e\u6027&#xff09;<\/li>\n<li>progressBlock \u548c completionBlock \u90fd\u6301\u6709 ViewController&#xff08;\u901a\u8fc7 self&#xff09;<\/li>\n<li>\u5f62\u6210\u5faa\u73af\u5f15\u7528\u94fe&#xff1a;ViewController -&gt; downloadManager -&gt; Block -&gt; ViewController<\/li>\n<li>\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f&#xff1a;\u8fd9\u4e9b\u5bf9\u8c61\u90fd\u65e0\u6cd5\u88ab\u91ca\u653e<\/li>\n<h5>\u89e3\u51b3\u65b9\u6848&#xff1a;<\/h5>\n<li>\u4f7f\u7528 __weak \u4fee\u9970\u7b26\u6253\u7834\u5faa\u73af\u5f15\u7528<\/li>\n<li>\u5728 Block \u5185\u90e8\u4f7f\u7528 weak-strong dance \u786e\u4fdd\u5bf9\u8c61\u4e0d\u4f1a\u88ab\u91ca\u653e<\/li>\n<li>\u6ce8\u610f\u5bf9\u8c61\u4e4b\u95f4\u7684\u5f15\u7528\u5173\u7cfb&#xff0c;\u5408\u7406\u4f7f\u7528 weak \u5c5e\u6027<\/li>\n<h5>\u6b63\u786e\u5199\u6cd5&#xff1a;<\/h5>\n<p>&#064;implementation ViewController<br \/>\n&#8211; (void)setup {<br \/>\n    self.downloadManager &#061; [[DownloadManager alloc] init];<\/p>\n<p>    \/\/ \u4f7f\u7528 weak \u5f15\u7528\u6253\u7834\u5faa\u73af<br \/>\n    __weak typeof(self) weakSelf &#061; self;<\/p>\n<p>    \/\/ \u4e0b\u8f7d\u8fdb\u5ea6\u56de\u8c03<br \/>\n    self.downloadManager.progressBlock &#061; ^(CGFloat progress) {<br \/>\n        __strong typeof(weakSelf) strongSelf &#061; weakSelf;<br \/>\n        [strongSelf updateProgress:progress];<br \/>\n    };<\/p>\n<p>    \/\/ \u4e0b\u8f7d\u5b8c\u6210\u56de\u8c03<br \/>\n    self.downloadManager.completionBlock &#061; ^{<br \/>\n        __strong typeof(weakSelf) strongSelf &#061; weakSelf;<br \/>\n        [strongSelf handleDownloadComplete];<br \/>\n    };<br \/>\n}<br \/>\n&#064;end<\/p>\n<h4>4.3 \u5185\u5b58\u7ba1\u7406\u6ce8\u610f\u4e8b\u9879<\/h4>\n<ul>\n<li>\u5728 MRC \u73af\u5883\u4e0b&#xff0c;\u5806 Block \u9700\u8981\u624b\u52a8\u7ba1\u7406\u5185\u5b58&#xff08;copy\/release&#xff09;<\/li>\n<li>\u5728 ARC \u73af\u5883\u4e0b&#xff0c;\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u63d2\u5165\u9002\u5f53\u7684\u5185\u5b58\u7ba1\u7406\u4ee3\u7801<\/li>\n<li>\u4f7f\u7528 __block \u4fee\u9970\u7684\u53d8\u91cf\u5728 Block \u590d\u5236\u5230\u5806\u65f6\u4e5f\u4f1a\u88ab\u590d\u5236<\/li>\n<li>\u5bf9\u8c61\u7c7b\u578b\u7684\u53d8\u91cf\u5728 Block \u4e2d\u9ed8\u8ba4\u662f\u5f3a\u5f15\u7528&#xff0c;\u9700\u8981\u6ce8\u610f\u5faa\u73af\u5f15\u7528\u95ee\u9898<\/li>\n<\/ul>\n<hr \/>\n<h3>5. Block \u7684\u4f7f\u7528\u6280\u5de7<\/h3>\n<h4>5.1 \u5f02\u6b65\u7f16\u7a0b<\/h4>\n<p>\u5728 iOS \u5f00\u53d1\u4e2d&#xff0c;Block \u6700\u5e38\u7528\u7684\u573a\u666f\u4e4b\u4e00\u5c31\u662f\u5f02\u6b65\u7f16\u7a0b\u3002\u901a\u8fc7 Block \u53ef\u4ee5\u4f18\u96c5\u5730\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u7ed3\u679c&#xff0c;\u907f\u514d\u56de\u8c03\u5730\u72f1\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;\u6570\u636e\u52a0\u8f7d<\/p>\n<p>\/\/ \u5b9a\u4e49\u56de\u8c03 Block \u7c7b\u578b<br \/>\ntypedef void (^Completion)(id result, NSError *error);<\/p>\n<p>&#8211; (void)loadData:(Completion)completion {<br \/>\n    \/\/ \u5728\u540e\u53f0\u7ebf\u7a0b\u6267\u884c\u8017\u65f6\u64cd\u4f5c<br \/>\n    dispatch_async(dispatch_get_global_queue(0, 0), ^{<br \/>\n        id result &#061; [self fetchData];<br \/>\n        NSError *error &#061; nil;<\/p>\n<p>        \/\/ \u56de\u5230\u4e3b\u7ebf\u7a0b\u66f4\u65b0 UI<br \/>\n        dispatch_async(dispatch_get_main_queue(), ^{<br \/>\n            if (completion) {<br \/>\n                completion(result, error);<br \/>\n            }<br \/>\n        });<br \/>\n    });<br \/>\n}<\/p>\n<p>\u4f7f\u7528\u8bf4\u660e&#xff1a;<\/p>\n<li>\u4f7f\u7528 typedef \u5b9a\u4e49 Block \u7c7b\u578b&#xff0c;\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027<\/li>\n<li>\u5728\u540e\u53f0\u7ebf\u7a0b\u6267\u884c\u8017\u65f6\u64cd\u4f5c&#xff0c;\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b<\/li>\n<li>\u5728\u4e3b\u7ebf\u7a0b\u56de\u8c03&#xff0c;\u786e\u4fdd UI \u66f4\u65b0\u64cd\u4f5c\u5b89\u5168<\/li>\n<li>\u4f7f\u7528 if (completion) \u68c0\u67e5 Block \u662f\u5426\u5b58\u5728<\/li>\n<h4>5.2 \u94fe\u5f0f\u8c03\u7528<\/h4>\n<p>Block \u8fd8\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u94fe\u5f0f\u8c03\u7528\u8bed\u6cd5&#xff0c;\u4f7f\u4ee3\u7801\u66f4\u52a0\u6d41\u7545\u548c\u6613\u8bfb\u3002\u8fd9\u79cd\u6a21\u5f0f\u5728\u6784\u5efa\u5bf9\u8c61\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;Person \u5bf9\u8c61\u6784\u5efa<\/p>\n<p>&#064;interface Person : NSObject<br \/>\n&#8211; (Person *(^)(NSString *))name;<br \/>\n&#8211; (Person *(^)(NSInteger))age;<br \/>\n&#064;end<\/p>\n<p>&#064;implementation Person<br \/>\n&#8211; (Person *(^)(NSString *))name {<br \/>\n    return ^Person *(NSString *name) {<br \/>\n        _name &#061; name;<br \/>\n        return self;  \/\/ \u8fd4\u56de self \u5b9e\u73b0\u94fe\u5f0f\u8c03\u7528<br \/>\n    };<br \/>\n}<br \/>\n&#064;end<\/p>\n<p>\/\/ \u4f7f\u7528\u793a\u4f8b<br \/>\nPerson *person &#061; [[Person alloc] init];<br \/>\nperson.name(&#064;&#034;\u5f20\u4e09&#034;).age(20);  \/\/ \u94fe\u5f0f\u8c03\u7528<\/p>\n<p>\u4f7f\u7528\u8bf4\u660e&#xff1a;<\/p>\n<li>\u6bcf\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a Block&#xff0c;Block \u8fd4\u56de self<\/li>\n<li>\u901a\u8fc7\u8fd4\u56de self \u5b9e\u73b0\u94fe\u5f0f\u8c03\u7528<\/li>\n<li>\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01&#xff0c;\u53ef\u8bfb\u6027\u66f4\u597d<\/li>\n<li>\u9002\u5408\u6784\u5efa\u590d\u6742\u5bf9\u8c61\u65f6\u7684\u914d\u7f6e<\/li>\n<hr \/>\n<h3>6. Block \u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<h4>6.1 \u56de\u8c03\u5904\u7406<\/h4>\n<p>\u5728 iOS \u5f00\u53d1\u4e2d&#xff0c;Block \u5e7f\u6cdb\u7528\u4e8e\u5404\u79cd\u56de\u8c03\u573a\u666f&#xff0c;\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6570\u636e\u52a0\u8f7d\u7b49\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;\u7f51\u7edc\u8bf7\u6c42\u56de\u8c03<\/p>\n<p>&#8211; (void)requestData:(void (^)(NSArray *data, NSError *error))completion {<br \/>\n    [self.network requestWithCompletion:^(id response, NSError *error) {<br \/>\n        if (completion) {<br \/>\n            completion(response, error);<br \/>\n        }<br \/>\n    }];<br \/>\n}<\/p>\n<p>\u4f7f\u7528\u8bf4\u660e&#xff1a;<\/p>\n<li>\u4f7f\u7528 Block \u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u7ed3\u679c<\/li>\n<li>\u652f\u6301\u9519\u8bef\u5904\u7406\u548c\u6210\u529f\u56de\u8c03<\/li>\n<li>\u4ee3\u7801\u7ed3\u6784\u6e05\u6670&#xff0c;\u6613\u4e8e\u7ef4\u62a4<\/li>\n<h4>6.2 \u52a8\u753b\u5904\u7406<\/h4>\n<p>UIKit \u6846\u67b6\u4e2d\u7684\u52a8\u753b API \u5927\u91cf\u4f7f\u7528 Block \u6765\u5904\u7406\u52a8\u753b\u8fc7\u7a0b\u548c\u5b8c\u6210\u56de\u8c03\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;\u89c6\u56fe\u52a8\u753b<\/p>\n<p>[UIView animateWithDuration:0.3<br \/>\n                 animations:^{<br \/>\n                     \/\/ \u52a8\u753b\u8fc7\u7a0b\u4e2d\u7684\u5c5e\u6027\u4fee\u6539<br \/>\n                     view.alpha &#061; 0.5;<br \/>\n                     view.frame &#061; newFrame;<br \/>\n                 }<br \/>\n                 completion:^(BOOL finished) {<br \/>\n                     \/\/ \u52a8\u753b\u5b8c\u6210\u540e\u7684\u5904\u7406<br \/>\n                     if (finished) {<br \/>\n                         [view removeFromSuperview];<br \/>\n                     }<br \/>\n                 }];<\/p>\n<p>\u4f7f\u7528\u8bf4\u660e&#xff1a;<\/p>\n<li>animations Block \u5b9a\u4e49\u52a8\u753b\u8fc7\u7a0b<\/li>\n<li>completion Block \u5904\u7406\u52a8\u753b\u5b8c\u6210<\/li>\n<li>\u652f\u6301\u52a8\u753b\u53d6\u6d88\u548c\u5b8c\u6210\u72b6\u6001\u5224\u65ad<\/li>\n<h4>6.3 \u679a\u4e3e\u5904\u7406<\/h4>\n<p>Foundation \u6846\u67b6\u4e2d\u7684\u96c6\u5408\u7c7b\u63d0\u4f9b\u4e86\u57fa\u4e8e Block \u7684\u679a\u4e3e\u65b9\u6cd5&#xff0c;\u4f7f\u96c6\u5408\u64cd\u4f5c\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;\u6570\u7ec4\u904d\u5386<\/p>\n<p>[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {<br \/>\n    \/\/ \u5904\u7406\u6bcf\u4e2a\u5143\u7d20<br \/>\n    if ([obj isEqual:targetObject]) {<br \/>\n        *stop &#061; YES;  \/\/ \u53ef\u4ee5\u63d0\u524d\u7ec8\u6b62\u904d\u5386<br \/>\n    }<br \/>\n}];<\/p>\n<p>\u4f7f\u7528\u8bf4\u660e&#xff1a;<\/p>\n<li>\u652f\u6301\u83b7\u53d6\u5143\u7d20\u7d22\u5f15<\/li>\n<li>\u53ef\u4ee5\u901a\u8fc7 stop \u53c2\u6570\u63d0\u524d\u7ec8\u6b62\u904d\u5386<\/li>\n<li>\u6bd4\u4f20\u7edf\u7684 for \u5faa\u73af\u66f4\u52a0\u7075\u6d3b<\/li>\n<hr \/>\n<h3>7. Block \u6027\u80fd\u4f18\u5316\u4e0e\u8c03\u8bd5\u6280\u5de7<\/h3>\n<h4>7.1 \u6027\u80fd\u4f18\u5316\u5efa\u8bae<\/h4>\n<li>\n<p>\u907f\u514d\u4e0d\u5fc5\u8981\u7684 Block \u590d\u5236 \u5168\u5c40 Block \u65e0\u9700 copy&#xff0c;\u6808 Block \u4ec5\u5728\u9700\u8981\u65f6\u590d\u5236\u5230\u5806&#xff0c;\u6ce8\u610f __block \u53d8\u91cf\u4f1a\u968f Block \u4e00\u8d77\u590d\u5236\u3002<\/p>\n<\/li>\n<li>\n<p>\u51cf\u5c11\u6355\u83b7\u53d8\u91cf\u6570\u91cf \u4ec5\u6355\u83b7\u5fc5\u8981\u53d8\u91cf&#xff0c;\u907f\u514d\u5927\u578b\u5bf9\u8c61&#xff0c;\u9ad8\u9891\u573a\u666f\u4f18\u5148\u4f7f\u7528\u5168\u5c40 Block\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f3a\u5f31\u5f15\u7528\u7b56\u7565 \u5916\u90e8\u7528 __weak \u9632\u5faa\u73af\u5f15\u7528&#xff0c;\u5185\u90e8\u8f6c __strong \u4fdd\u5bf9\u8c61\u5b58\u6d3b&#xff0c;\u907f\u514d\u5185\u90e8\u9891\u7e41\u521b\u5efa\u4e34\u65f6\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>\n<p>\u5185\u5b58\u5e03\u5c40\u4f18\u5316 \u9ad8\u9891\u8bbf\u95ee\u53d8\u91cf\u524d\u7f6e&#xff0c;\u51cf\u5c11\u6355\u83b7\u53d8\u91cf\u603b\u6570&#xff0c;\u8bc4\u4f30 __block \u53d8\u91cf\u7684\u66ff\u4ee3\u65b9\u6848\u3002<\/p>\n<\/li>\n<h4>7.2 \u8c03\u8bd5\u6280\u5de7<\/h4>\n<li>\n<p>Instruments \u4e09\u5251\u5ba2 Allocations \u8ffd\u8e2a\u5185\u5b58\u751f\u547d\u5468\u671f&#xff0c;Leaks \u68c0\u6d4b\u5faa\u73af\u5f15\u7528&#xff0c;Time Profiler \u5206\u6790\u6267\u884c\u8017\u65f6<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528 LLDB \u8c03\u8bd5 Block<\/p>\n<p> (lldb) po block  # \u6253\u5370 Block \u5bf9\u8c61<br \/>\n(lldb) p\/x (long)block-&gt;isa   # \u67e5\u770b Block \u7684\u7c7b\u578b<br \/>\n(lldb) p\/x (int)block-&gt;flags   # \u67e5\u770b Block \u7684 flags<br \/>\n(lldb) p *(struct __block_impl *)block   # \u67e5\u770b Block \u6355\u83b7\u7684\u53d8\u91cf\n <\/li>\n<li>\n<p>\u95ee\u9898\u6392\u67e5\u4e09\u677f\u65a7<\/p>\n<ul>\n<li>\u7c7b\u578b\u9a8c\u8bc1&#xff1a;po [block class] &#043; \u68c0\u67e5 flags \u5224\u65ad\u5806\u6808\u7c7b\u578b<\/li>\n<li>\u53d8\u91cf\u5ba1\u8ba1&#xff1a;p\/x \u67e5\u770b\u5730\u5740\u504f\u79fb&#xff0c;\u9a8c\u8bc1\u590d\u5236\u884c\u4e3a<\/li>\n<li>\u5185\u5b58\u9a8c\u8bc1&#xff1a;Xcode Memory Graph \u53ef\u89c6\u5316\u5f15\u7528&#xff0c;malloc_history \u8ffd\u6eaf\u5206\u914d\u5806\u6808<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5de5\u5177\u94fe\u63a8\u8350<\/p>\n<ul>\n<li>\u57fa\u7840\u5206\u6790&#xff1a;Instruments \u4e09\u4ef6\u5957&#xff08;Allocations\/Leaks\/Time Profiler&#xff09;<\/li>\n<li>\u6df1\u5ea6\u8c03\u8bd5&#xff1a;Memory Graph \u76f4\u89c2\u6d4b\u5faa\u73af\u5f15\u7528&#xff0c;MallocStackLogging \u5b9a\u4f4d\u91ce\u6307\u9488<\/li>\n<\/ul>\n<\/li>\n<h3>8. \u603b\u7ed3<\/h3>\n<p>Block \u662f Objective-C \u4e2d\u7684\u7279\u6b8a\u5bf9\u8c61&#xff0c;\u901a\u8fc7 isa \u6307\u9488\u7ee7\u627f\u81ea NSObject&#xff0c;\u5b9e\u73b0\u4e86\u533f\u540d\u51fd\u6570\u7684\u529f\u80fd\u3002\u5b83\u6709\u4e09\u79cd\u7c7b\u578b&#xff1a;\u5168\u5c40 Block&#xff08;\u4e0d\u6355\u83b7\u53d8\u91cf&#xff0c;\u5b58\u50a8\u5728\u6570\u636e\u533a&#xff09;\u3001\u6808 Block&#xff08;\u6355\u83b7\u53d8\u91cf&#xff0c;\u5b58\u50a8\u5728\u6808\u4e0a&#xff09;\u548c\u5806 Block&#xff08;\u7531\u6808 Block \u590d\u5236\u800c\u6765&#xff0c;\u5b58\u50a8\u5728\u5806\u4e0a&#xff09;\u3002Block \u53ef\u4ee5\u6355\u83b7\u53d8\u91cf&#xff0c;\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u91c7\u7528\u503c\u590d\u5236&#xff0c;\u4f7f\u7528 __block \u4fee\u9970\u7b26\u53ef\u4ee5\u4fee\u6539\u6355\u83b7\u7684\u53d8\u91cf&#xff0c;\u5bf9\u8c61\u7c7b\u578b\u9ed8\u8ba4\u662f\u5f3a\u5f15\u7528\u3002\u5728\u5185\u5b58\u7ba1\u7406\u65b9\u9762&#xff0c;Block \u9075\u5faa Objective-C \u7684\u5185\u5b58\u7ba1\u7406\u89c4\u5219&#xff0c;\u9700\u8981\u6ce8\u610f\u5faa\u73af\u5f15\u7528\u95ee\u9898&#xff0c;\u53ef\u4ee5\u901a\u8fc7 __weak \u548c weak-strong dance \u6a21\u5f0f\u89e3\u51b3\u3002Block \u7684\u7c7b\u578b\u7f16\u7801\u4f7f\u7528 &#064;?&lt;\u8fd4\u56de\u503c\u7c7b\u578b\u53c2\u6570\u7c7b\u578b&gt; \u7684\u683c\u5f0f&#xff0c;\u652f\u6301\u57fa\u672c\u7c7b\u578b\u3001\u5bf9\u8c61\u7c7b\u578b\u548c Block \u7c7b\u578b\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;Block \u5e7f\u6cdb\u5e94\u7528\u4e8e\u5f02\u6b65\u7f16\u7a0b\u3001\u56de\u8c03\u5904\u7406\u3001\u52a8\u753b\u548c\u96c6\u5408\u64cd\u4f5c\u7b49\u573a\u666f&#xff0c;\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u7075\u6d3b\u3002<\/p>\n<hr \/>\n<p>\u5982\u679c\u89c9\u5f97\u672c\u6587\u5bf9\u4f60\u6709\u5e2e\u52a9&#xff0c;\u6b22\u8fce\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u5173\u6ce8\u6211&#xff0c;\u540e\u7eed\u4f1a\u6301\u7eed\u5206\u4eab\u66f4\u591a iOS \u5e95\u5c42\u539f\u7406\u4e0e\u5b9e\u6218\u7ecf\u9a8c&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.9k\u6b21\uff0c\u70b9\u8d5e17\u6b21\uff0c\u6536\u85cf31\u6b21\u3002Block \u662f Objective-C \u4e2d\u7684\u7279\u6b8a\u5bf9\u8c61\uff0c\u901a\u8fc7 isa \u6307\u9488\u7ee7\u627f\u81ea NSObject\uff0c\u5b9e\u73b0\u4e86\u533f\u540d\u51fd\u6570\u7684\u529f\u80fd\u3002\u5b83\u6709\u4e09\u79cd\u7c7b\u578b\uff1a\u5168\u5c40 Block\uff08\u4e0d\u6355\u83b7\u53d8\u91cf\uff0c\u5b58\u50a8\u5728\u6570\u636e\u533a\uff09\u3001\u6808 Block\uff08\u6355\u83b7\u53d8\u91cf\uff0c\u5b58\u50a8\u5728\u6808\u4e0a\uff09\u548c\u5806 Block\uff08\u7531\u6808 Block \u590d\u5236\u800c\u6765\uff0c\u5b58\u50a8\u5728\u5806\u4e0a\uff09\u3002Block \u53ef\u4ee5\u6355\u83b7\u53d8\u91cf\uff0c\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u91c7\u7528\u503c\u590d\u5236\uff0c\u4f7f\u7528__block\u4fee\u9970\u7b26\u53ef\u4ee5\u4fee\u6539\u6355\u83b7\u7684\u53d8\u91cf\uff0c\u5bf9\u8c61\u7c7b\u578b\u9ed8\u8ba4\u662f\u5f3a\u5f15\u7528\u3002\u5728\u5185\u5b58\u7ba1\u7406\u65b9\u9762\uff0cBlock \u9075\u5faa Objective-C \u7684\u5185\u5b58\u7ba1\u7406\u89c4\u5219\uff0c\u9700\u8981\u6ce8\u610f\u5faa\u73af\u5f15\u7528\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7__weak\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1680,3372,190,3371],"topic":[],"class_list":["post-38441","post","type-post","status-publish","format-standard","hentry","category-server","tag-ios","tag-objective-c","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Objective-C Block \u5e95\u5c42\u539f\u7406\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/38441.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Objective-C Block \u5e95\u5c42\u539f\u7406\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.9k\u6b21\uff0c\u70b9\u8d5e17\u6b21\uff0c\u6536\u85cf31\u6b21\u3002Block \u662f Objective-C \u4e2d\u7684\u7279\u6b8a\u5bf9\u8c61\uff0c\u901a\u8fc7 isa \u6307\u9488\u7ee7\u627f\u81ea NSObject\uff0c\u5b9e\u73b0\u4e86\u533f\u540d\u51fd\u6570\u7684\u529f\u80fd\u3002\u5b83\u6709\u4e09\u79cd\u7c7b\u578b\uff1a\u5168\u5c40 Block\uff08\u4e0d\u6355\u83b7\u53d8\u91cf\uff0c\u5b58\u50a8\u5728\u6570\u636e\u533a\uff09\u3001\u6808 Block\uff08\u6355\u83b7\u53d8\u91cf\uff0c\u5b58\u50a8\u5728\u6808\u4e0a\uff09\u548c\u5806 Block\uff08\u7531\u6808 Block \u590d\u5236\u800c\u6765\uff0c\u5b58\u50a8\u5728\u5806\u4e0a\uff09\u3002Block \u53ef\u4ee5\u6355\u83b7\u53d8\u91cf\uff0c\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u91c7\u7528\u503c\u590d\u5236\uff0c\u4f7f\u7528__block\u4fee\u9970\u7b26\u53ef\u4ee5\u4fee\u6539\u6355\u83b7\u7684\u53d8\u91cf\uff0c\u5bf9\u8c61\u7c7b\u578b\u9ed8\u8ba4\u662f\u5f3a\u5f15\u7528\u3002\u5728\u5185\u5b58\u7ba1\u7406\u65b9\u9762\uff0cBlock \u9075\u5faa Objective-C \u7684\u5185\u5b58\u7ba1\u7406\u89c4\u5219\uff0c\u9700\u8981\u6ce8\u610f\u5faa\u73af\u5f15\u7528\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7__weak\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/38441.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-20T05:02:37+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38441.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/38441.html\",\"name\":\"Objective-C Block \u5e95\u5c42\u539f\u7406\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-20T05:02:37+00:00\",\"dateModified\":\"2025-05-20T05:02:37+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/38441.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/38441.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38441.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Objective-C Block \u5e95\u5c42\u539f\u7406\u6df1\u5ea6\u89e3\u6790\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Objective-C Block \u5e95\u5c42\u539f\u7406\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/38441.html","og_locale":"zh_CN","og_type":"article","og_title":"Objective-C Block \u5e95\u5c42\u539f\u7406\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.9k\u6b21\uff0c\u70b9\u8d5e17\u6b21\uff0c\u6536\u85cf31\u6b21\u3002Block \u662f Objective-C \u4e2d\u7684\u7279\u6b8a\u5bf9\u8c61\uff0c\u901a\u8fc7 isa \u6307\u9488\u7ee7\u627f\u81ea NSObject\uff0c\u5b9e\u73b0\u4e86\u533f\u540d\u51fd\u6570\u7684\u529f\u80fd\u3002\u5b83\u6709\u4e09\u79cd\u7c7b\u578b\uff1a\u5168\u5c40 Block\uff08\u4e0d\u6355\u83b7\u53d8\u91cf\uff0c\u5b58\u50a8\u5728\u6570\u636e\u533a\uff09\u3001\u6808 Block\uff08\u6355\u83b7\u53d8\u91cf\uff0c\u5b58\u50a8\u5728\u6808\u4e0a\uff09\u548c\u5806 Block\uff08\u7531\u6808 Block \u590d\u5236\u800c\u6765\uff0c\u5b58\u50a8\u5728\u5806\u4e0a\uff09\u3002Block \u53ef\u4ee5\u6355\u83b7\u53d8\u91cf\uff0c\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u91c7\u7528\u503c\u590d\u5236\uff0c\u4f7f\u7528__block\u4fee\u9970\u7b26\u53ef\u4ee5\u4fee\u6539\u6355\u83b7\u7684\u53d8\u91cf\uff0c\u5bf9\u8c61\u7c7b\u578b\u9ed8\u8ba4\u662f\u5f3a\u5f15\u7528\u3002\u5728\u5185\u5b58\u7ba1\u7406\u65b9\u9762\uff0cBlock \u9075\u5faa Objective-C \u7684\u5185\u5b58\u7ba1\u7406\u89c4\u5219\uff0c\u9700\u8981\u6ce8\u610f\u5faa\u73af\u5f15\u7528\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7__weak\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/38441.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-20T05:02:37+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/38441.html","url":"https:\/\/www.wsisp.com\/helps\/38441.html","name":"Objective-C Block \u5e95\u5c42\u539f\u7406\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-20T05:02:37+00:00","dateModified":"2025-05-20T05:02:37+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/38441.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/38441.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/38441.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Objective-C Block \u5e95\u5c42\u539f\u7406\u6df1\u5ea6\u89e3\u6790"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=38441"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38441\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=38441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=38441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=38441"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=38441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}