三种Swift实现单例模式的方法:全局变量,内部变量,dispatch_once方式

1. 全局变量

private let _singleton = Singleton()  

class Singleton: NSObject {
class var sharedInstance: Singleton {
get {
return _singleton
}
}
}

  

2. 内部变量

class Singleton {
class var sharedInstance: Singleton {
get {
struct SingletonStruct {
static let singleton: Singleton = Singleton()
}
return SingletonStruct.singleton
}
}
}

  

3. dispatch_once方式

class Singleton {
class var sharedInstance: Singleton {
get {
struct SingletonStruct {
static var onceToken:dispatch_once_t = 0
static var singleton: Singleton? = nil
}
dispatch_once(&SingletonStruct.onceToken, { () -> Void in
SingletonStruct.singleton = Singleton()
})
return SingletonStruct.singleton!
}
}
}

  

Swift之单例模式的更多相关文章

  1. Swift实战-单例模式

    设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.GoF提出了23种设计模式,本系列将使用Swift语言来实现这些设计模式 概述 整个应用生命 ...

  2. swift 创建单例模式

    一.意图 保证一个类公有一个实例,并提供一个访问它的全局访问点. 二.使用场景 1.使用场景 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时 当这个唯一实例应该是通过子类化可扩展的,并且 ...

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

    转自:点击打开链接 From my short experience with Swift there are three approaches to implement the Singleton ...

  4. swift语言实现单例模式

    Swift实现单例模式 单例模式在各个语言中都有实现,swift语言推出已经几天了.通过这几天的看文档.特奉上写的Swift的单例实现,供大家学习交流,欢迎指正. ---若转载请注明出处,本人Gith ...

  5. 单例模式(Singleton Pattern)

    意图 保证一个类仅有一个实例,并提供一个该实例的全局访问点 可将一个实例扩展到n个实例.限定某类最多只能创建n个实例. 双重锁定实现单例模式 C# public sealed class Single ...

  6. iOS开发——Swift篇&单例的实现

    Swift实现单例模式 Swift实现单例模式 由于Swift语言弱化了struct和class之间的界限,这里我分别给出自己写的两种的单例实现 class版本: class SwiftSinglet ...

  7. iOS四种多线程(swift和oc)

    在这篇文章中,我将为你整理一下 iOS 开发中几种多线程方案,以及其使用方法和注意事项.当然也会给出几种多线程的案例,在实际使用中感受它们的区别.还有一点需要说明的是,这篇文章将会使用 Swift 和 ...

  8. 用Swift实现一款天气预报APP(二)

    这个系列的目录: 用Swift实现一款天气预报APP(一) 用Swift实现一款天气预报APP(二) 用Swift实现一款天气预报APP(三) 上篇中主要讲了界面的一些内容,这篇主要讨论网络请求,获得 ...

  9. iOS多线程学习

    在 iOS 中其实目前有 4 套多线程方案,他们分别是: Pthreads NSThread GCD NSOperation & NSOperationQueue 所以接下来,我会一一讲解这些 ...

随机推荐

  1. RadioButton 带下划线切换的案例

    xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id=& ...

  2. Two Sum ——经典的哈希表的题

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  3. Linux下cp命令的使用说明

    [root@www ~]# cp [-adfilprsu] 来源档(source) 目标档(destination)[root@www ~]# cp [options] source1 source2 ...

  4. node修改全局环境路径 与 全局后出现sh:exe command not found

    修改全局环境路径 当安装nodeJs时候需要修改全局环境的指向,先看看npm config get prefix  全局环境在哪里 然后执行更换命令,一个是主文件一个是缓存文件 npm config ...

  5. 解决CentOS7 firefox崩溃问题

    现象:我在博客园写随笔的时候插入图片,每次都会崩溃,百度了好久,发现这个方法还挺好用的 在终端敲入如下命令: setsebool -P unconfined_mozilla_plugin_transi ...

  6. php smtp发送邮件功能

    <?php header("Content-Type: text/html; charset=utf-8"); class smtp { /* Public Variable ...

  7. centos6.5 安装scrapy

    1. 安装Twisted, 下载安装包 python setup.py install 2. yum install libffi-devel python-devel 3 pip install s ...

  8. 洛谷P1007 独木桥 [数论]

    题目传送门 独木桥 题目背景 战争已经进入到紧要时间.你是运输小队长,正在率领运输部队向前线运送物资.运输任务像做题一样的无聊.你希望找些刺激,于是命令你的士兵们到前方的一座独木桥上欣赏风景,而你留在 ...

  9. Python3 文件基本修改替换

    现有原文件: Somehow, it seems the love I knew was always the most destructive kind 不知为何,我经历的爱情总是最具毁灭性的的那种 ...

  10. AndroidManifest.xml文件详解(permission)

    http://blog.csdn.net/think_soft/article/details/7574726 语法(SYNTAX): <permissionandroid:descriptio ...