IOS Single case
Last Update:2015-07-17
Source: Internet
Author: User
<span id="Label3"></p>IOS Single case<p style="margin-top: 1em; margin-right: 0; margin-bottom: 1em; margin-left: 0; line-height: 1.8em;"><p style="margin-top: 1em; margin-right: 0; margin-bottom: 1em; margin-left: 0; line-height: 1.8em;">1. Official Document Wording</p></p><pre style="word-wrap: break-word; margin-top: 15px; margin-right: 0; margin-bottom: 15px; margin-left: 0; background-color: #F8F8F8; border: 1px solid #CCCCCC; border-radius: 3px; overflow: auto; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px;"><code class="hljs objectivec" style="background-color: #F8F8F8; border: 0; border-radius: 4px; color: #4f424c; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; font-family: Consolas , Monaco , Andale Mono , monospace; line-height: 1; margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0;"><code class="hljs objectivec" style="background-color: #F8F8F8; border:0; border-radius:4px; Color: #4f424c; font-size:90%; padding-top:0.5em; padding-right:0.5em; padding-bottom:0.5em; padding-left:0.5em; display:block; Overflow-x: auto; Background: #e7e9db; -webkit-text-size-adjust:none; font-family:consolas, Monaco, Andale Mono, monospace; line-height:1; margin-top:0; margin-right:0; margin-bottom:0; margin-left:0; "><span class="hljs-keyword" style="color: #815ba4;">static </span> accountmanager *defaultmanager = <span class=" Hljs-literal " style=" color: #f99b15; ">nil </span>; + (accountmanager *) defaultmanager {<span class="hljs-keyword" style="color: #815ba4;">if </span> (! Defaultmanager) Defaultmanager = [[<span class="hljs-keyword" style="color: #815ba4;">self </span> allocwithzone:< Span class= "hljs-literal" style= "color: #f99b15;" >null ] init]; <span class="hljs-keyword" style="color: #815ba4;">return </span> defaultmanager;} </code></code></pre><p style="margin-top: 1em; margin-right: 0; margin-bottom: 1em; margin-left: 0; line-height: 1.8em;"><p style="margin-top: 1em; margin-right: 0; margin-bottom: 1em; margin-left: 0; line-height: 1.8em;">2. from objcolumnist</p></p><pre style="word-wrap: break-word; margin-top: 15px; margin-right: 0; margin-bottom: 15px; margin-left: 0; background-color: #F8F8F8; border: 1px solid #CCCCCC; border-radius: 3px; overflow: auto; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px;"><code class="hljs objectivec" style="background-color: #F8F8F8; border: 0; border-radius: 4px; color: #4f424c; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; font-family: Consolas , Monaco , Andale Mono , monospace; line-height: 1; margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0;">+ (accountmanager *) sharedmanager{<span class="hljs-keyword" style="color: #815ba4;"><span class="hljs-keyword" style="color: #815ba4;">Static</span></span>Accountmanager *sharedaccountmanagerinstance =<span class="hljs-literal" style="color: #f99b15;"><span class="hljs-literal" style="color: #f99b15;">Nil</span></span>;<span class="hljs-keyword" style="color: #815ba4;"><span class="hljs-keyword" style="color: #815ba4;">Static</span></span> <span class="hljs-built_in" style="color: #f99b15;"><span class="hljs-built_in" style="color: #f99b15;">dispatch_once_t</span></span>predicate;<span class="hljs-built_in" style="color: #f99b15;"><span class="hljs-built_in" style="color: #f99b15;">dispatch_once</span></span>(&predicate, ^{sharedaccountmanagerinstance = [[<span class="hljs-keyword" style="color: #815ba4;"><span class="hljs-keyword" style="color: #815ba4;"></span> self</span>alloc] init]; });<span class="hljs-keyword" style="color: #815ba4;"><span class="hljs-keyword" style="color: #815ba4;">return</span></span>sharedaccountmanagerinstance;}</code></pre><p style="margin-top: 1em; margin-right: 0; margin-bottom: 1em; margin-left: 0; line-height: 1.8em;"><p style="margin-top: 1em; margin-right: 0; margin-bottom: 1em; margin-left: 0; line-height: 1.8em;">The notation satisfies three features:<br>1) Thread Safety.<br>2) meet the requirements of the static analyzer.<br>3) compatible with ARC</p></p><p style="margin-top: 1em; margin-right: 0; margin-bottom: 1em; margin-left: 0; line-height: 1.8em;"><p style="margin-top: 1em; margin-right: 0; margin-bottom: 1em; margin-left: 0; line-height: 1.8em;">Reference:<br>Two ways to create a single case with IOS</p></p><p><p>IOS Single case</p></p></span>