Swift单例写法(推荐)

来源:互联网 发布:数据的分散程度怎么求 编辑:程序博客网 时间:2024/05/12 14:11

写法优点:

  1. 实现方式与系统的单例一致;
  2. 支持任意版本的;
  3. 这种写法是线程安全的。
import UIKitclass TimeCountdown: NSObject {    class func shareInstance() -> TimeCountdown {        struct singleton {            static var predicate: dispatch_once_t = 0            static var instance: TimeCountdown? = nil        }        dispatch_once(&singleton.predicate, { () -> Void in            singleton.instance = TimeCountdown()        })        return singleton.instance!    }}
0 0
原创粉丝点击