Swift中如何安全的创建一个单例-爱代码爱编程
Talk is cheap. Show me the code.
class TestShareInstance{
var age:Int
static let shareInstane:TestShareInstance = TestShareInstance(age: 3);
private init(age:Int){
self.age = age;
};
}
说说原理
1. swift在类中,类变量是能够保证线程安全,swift底层,static关键字的实际上是使用`dispatch_once`语法来实现的,如下一段swift编译中间产物SIL语言中的代码就能看到底层的实现.staic变量被声明为全局变量
// static TestShareInstance.shareInstane
sil_global hidden [let] @static main.TestShareInstance.shareInstane : main.TestShareInstance : $TestShareInstance
2.在get方法中获取变量调用了swift内嵌的builtin "once",实际上调用的是`swift_once`方法
3.在swift源码中可以搜索到`swift_once`方法的内部实现如下,内部调用的就是GCD底层的`dispatch_once_f`,保证了单例的线程安全。
```
/// Runs the given function with the given context argument exactly once.
/// The predicate argument must point to a global or static variable of static
/// extent of type swift_once_t.
void swift::swift_once(swift_once_t *predicate, void (*fn)(void *),
void *context) {
#if defined(__APPLE__)
dispatch_once_f(predicate, context, fn);
#elif defined(__CYGWIN__)
_swift_once_f(predicate, context, fn);
#else
std::call_once(*predicate, [fn, context]() { fn(context); });
#endif
}
```
在类中,将TestShareInstance的init方法设置为了private属性,这能保证其他人没有办法调用你的init的方法,只能调用你的shareInstane单例变量;从而保证了单例的不可修改特性。如果你要强行修改,编译器就会警告你,具体示例如下
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/a2025131311/article/details/111057732