swift 单例定义

前端之家收集整理的这篇文章主要介绍了swift 单例定义前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


方法一:

import Foundation


class SingleOne {

//单例
static let shareSingleOne = SingleOne()

}


方法二:

import Foundation

class SingleTwo {

//单例
class func shareSingleTwo()->SingleTwo{
struct Singleton{
static var onceToken : dispatch_once_t = 0
static var single:SingleTwo?
}
dispatch_once(&Singleton.onceToken,{
Singleton.single=shareSingleTwo()
}
)
return Singleton.single!
}

}


方法三:

import Foundation

//全局的常量
let single = SingleThree()

class SingleThree {

class var sharedInstance : SingleThree {
return single
}
}


方法四:

import Foundation class SingleFour { static var sharedInstance : SingleFour { struct Static { static let instance : SingleFour = SingleFour() } return Static.instance } }

原文链接:https://www.f2er.com/swift/325743.html

猜你在找的Swift相关文章