swift单例创建的几种方法

//单例方法1
class SingleTonOne{
    static var sharedInstanceOne:SingleTonOne{
     struct SingleTonStruct {
     static var onceToken:dispatch_once_t = 0
     static var staticInstanceOne:SingleTonOne? = nil
    }
    dispatch_once(&SingleTonStruct.onceToken) {
            SingleTonStruct.staticInstanceOne = SingleTonOne()
        }
        
        return SingleTonStruct.staticInstanceOne!
    }
    
}

//单例方法2

class SingleTonTwo{
    class var sharedInstanceTwo:SingleTonTwo{
        struct StaticStruct {
            static let staticInstanceTwo:SingleTonTwo = SingleTonTwo()
        }
        
        return StaticStruct.staticInstanceTwo
    
    }

}

//单例方法3

class SingleTonThree{
    class var sharedInstance:SingleTonThree{
        return sharedInstanceThree
    }

}
private let sharedInstanceThree = SingleTonThree()


//单例方法4

class SingleTonFour{
   static let sharedInstanceFour = SingleTonFour()
    private init(){}

}

  

posted @ 2016-04-18 15:14  曹县三胖暴打大猩猩  阅读(299)  评论(0编辑  收藏  举报