关闭

Swift 实现单例模式Singleton pattern的三种方法

66人阅读 评论(0) 收藏 举报
分类:


From my short experience with Swift there are three approaches to implement the Singleton pattern that support lazy initialization and thread safety.

These approaches might change or become redundant as the language matures.

Global constant

<code style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline; white-space: inherit;"><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">let</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">_SingletonSharedInstance</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">=</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">()</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">

</span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">class</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">  </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
    </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">class</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">var</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> sharedInstance </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">:</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">return</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">_SingletonSharedInstance</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
    </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span></code>

We use a global constant because class constants are not yet supported.

This approach supports lazy initialization because Swift lazily initializes global constants (and variables), and is thread safe by virtue of let.

Nested struct

<code style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline; white-space: inherit;"><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">class</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
    </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">class</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">var</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> sharedInstance </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">:</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">struct</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Static</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
            </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">static</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">let</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> instance </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">:</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">=</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">()</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">return</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Static</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">.</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">instance
    </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span></code>

Unlike classes, structs do support static constants. By using a nested struct we can leverage its static constant as a class constant.

dispatch_once

The traditional Objective-C approach ported to Swift. I'd say it's no longer necessary to use this approach but I'm putting it here anyway as I find the differences in syntax interesting.

<code style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline; white-space: inherit;"><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">class</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
    </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">class</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">var</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> sharedInstance </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">:</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">struct</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Static</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
            </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">static</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">var</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> onceToken </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">:</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">dispatch_once_t</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">=</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="lit" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(128, 0, 0); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">0</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
            </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">static</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">var</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> instance </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">:</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">?</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">=</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">nil</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        dispatch_once</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">(&</span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Static</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">.</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">onceToken</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">)</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
            </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Static</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">.</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">instance </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">=</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">()</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">return</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Static</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">.</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">instance</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">!</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
    </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span></code>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1006次
    • 积分:26
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:11篇
    • 译文:0篇
    • 评论:0条
    文章分类