答案 0 :(得分:1)
使用
static let sharedInstance = ModelManager()
然后将初始化程序声明为private
private init() {
database = FMDatabase(init: SomeObject)
}
答案 1 :(得分:0)
尝试这样做:
class SomeSingleton {
internal static let sharedInstance = SomeSingleton()
// Your singleton properties
var name = ""
private init() { }
}
然后您可以访问它:
SomeSingleton.sharedInstance.name = "John"
let currentName = SomeSingleton.sharedInstance.name
答案 2 :(得分:0)
不允许您在类或静态函数上访问实例属性。解决方法是使sharedInstance属性成为静态属性。
static let sharedInstance = ModelManager()
答案 3 :(得分:0)
返回共享实例的方法
class var sharedInstance :YourClass {
struct Singleton {
static let instance = YourClass()
}
return Singleton.instance
}
访问共享实例:
let sharedInstance = YourClass.sharedInstance()
答案 4 :(得分:0)
单例实例:使用带有类的计算属性的结构
class MyClass :NSObject{
class var sharedInstance : MyClass {
struct Singleton {
static let instance = MyClass()
}
return Singleton.instance
}
// do other stuff here
func getCustomerLists()-> Any{
// return
}
}
访问:
MyClass.sharedInstance. getCustomerLists()
答案 5 :(得分:0)
尝试编写下面的格式: -
class var sharedInstance: YourClass {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: YourClass? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = YourClass()
}
return Static.instance!
}