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



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>
阅读更多
个人分类: Cocoa Touch
下一篇ios view的frame和bounds之区别(位置和大小)
想对作者说点什么? 我来说一句

Singleton pattern单例模式应用

2012年01月13日 82KB 下载

没有更多推荐了,返回首页

关闭
关闭