首页 > 代码库 > 简化只执行一次的写法

简化只执行一次的写法

<h4 id="toc_0">标准的写法</h4>

<div class="codehilite"><pre><span class="err">空</span>

</pre></div>


<h4 id="toc_1">简化后的宏</h4>

<div class="codehilite"><pre><span class="sr">/**************************************************************/</span>

<span class="sr">//</span> <span class="err">执行一次</span>

<span class="c1">#undef XY_ONCE_BEGIN</span>

<span class="c1">#define XY_ONCE_BEGIN( __name ) \</span>

<span class="n">static</span> <span class="n">dispatch_once_t</span> <span class="n">once_</span><span class="c1">##__name; \</span>

<span class="n">dispatch_once</span><span class="p">(</span> <span class="o">&amp;</span><span class="n">once_</span><span class="c1">##__name , ^{</span>


<span class="c1">#undef XY_ONCE_END</span>

<span class="c1">#define XY_ONCE_END });</span>

</pre></div>


<h4 id="toc_2">简化后的写法</h4>

<div class="codehilite"><pre><span class="o">-</span> <span class="p">(</span><span class="no">IBAction</span><span class="p">)</span><span class="ss">clickOnce</span><span class="p">:(</span><span class="nb">id</span><span class="p">)</span><span class="n">sender</span> <span class="p">{</span>

<span class="no">XY_ONCE_BEGIN</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>

<span class="no">SHOWMBProgressHUD</span><span class="p">(</span><span class="err">@</span><span class="s2">&quot;only show once&quot;</span><span class="p">,</span> <span class="kp">nil</span><span class="p">,</span> <span class="kp">nil</span><span class="p">,</span> <span class="no">NO</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>

<span class="no">XY_ONCE_END</span>

<span class="p">}</span>

</pre></div>

简化只执行一次的写法