如何在 swift 中创建单例对象
How to Create Singleton Object in swift
我正在学习 swift 中的单例模式以及创建单例的有效方法 class 并找到了如下所示的最佳创建方法。
class SingletonClass{
static let sharedInstance = SingletonClass()
}
因为我使用了 let
语句,它是只读的 属性 并且必须是线程安全的,所以从 [=24= 开始不需要 dispatch_once() ] C.And static
用于使 sharedInstance
变量成为 class
我猜的变量。
但这如何保证在整个应用程序中只创建一个实例?有什么我遗漏的小事吗?
保证只创建一次的是关键字static。可以参考这篇文章:
https://thatthinginswift.com/singletons/
希望对您有所帮助。
The static keyword denotes that a member variable, or method, can be
accessed without requiring an instantiation of the class to which it
belongs. In simple terms, it means that you can call a method, even if
you've never created the object to which it belongs
如果你想防止实例化你的class(有效地限制使用仅单例),那么你将你的初始化程序标记为private
:
class SingletonClass {
static let shared = SingletonClass()
private init() {
// initializer code here
}
}
你是对的。您可能想阅读 Files and Initialization 关于如何在 Swift
中处理全局变量和静态变量
Swift 使用这种方法
Initialize lazily, run the initializer for a global the first time it
is referenced, similar to Java.
它说
it allows custom initializers, startup time in Swift scales cleanly
with no global initializers to slow it down, and the order of
execution is completely predictable.
The lazy initializer for a global variable (also for static members of
structs and enums) is run the first time that global is accessed, and
is launched as dispatch_once
to make sure that the initialization is
atomic. This enables a cool way to use dispatch_once in your code:
just declare a global variable with an initializer and mark it
private.
创建私有初始化,例如:
final class Singleton {
// Can't init is singleton
private init() { }
//MARK: Shared Instance
static let sharedInstance: Singleton = Singleton()
//MARK: Local Variable
var emptyStringArray : [String] = []
}
我正在学习 swift 中的单例模式以及创建单例的有效方法 class 并找到了如下所示的最佳创建方法。
class SingletonClass{
static let sharedInstance = SingletonClass()
}
因为我使用了 let
语句,它是只读的 属性 并且必须是线程安全的,所以从 [=24= 开始不需要 dispatch_once() ] C.And static
用于使 sharedInstance
变量成为 class
我猜的变量。
但这如何保证在整个应用程序中只创建一个实例?有什么我遗漏的小事吗?
保证只创建一次的是关键字static。可以参考这篇文章: https://thatthinginswift.com/singletons/
希望对您有所帮助。
The static keyword denotes that a member variable, or method, can be accessed without requiring an instantiation of the class to which it belongs. In simple terms, it means that you can call a method, even if you've never created the object to which it belongs
如果你想防止实例化你的class(有效地限制使用仅单例),那么你将你的初始化程序标记为private
:
class SingletonClass {
static let shared = SingletonClass()
private init() {
// initializer code here
}
}
你是对的。您可能想阅读 Files and Initialization 关于如何在 Swift
中处理全局变量和静态变量Swift 使用这种方法
Initialize lazily, run the initializer for a global the first time it is referenced, similar to Java.
它说
it allows custom initializers, startup time in Swift scales cleanly with no global initializers to slow it down, and the order of execution is completely predictable.
The lazy initializer for a global variable (also for static members of structs and enums) is run the first time that global is accessed, and is launched as
dispatch_once
to make sure that the initialization is atomic. This enables a cool way to use dispatch_once in your code: just declare a global variable with an initializer and mark it private.
创建私有初始化,例如:
final class Singleton {
// Can't init is singleton
private init() { }
//MARK: Shared Instance
static let sharedInstance: Singleton = Singleton()
//MARK: Local Variable
var emptyStringArray : [String] = []
}