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;
 };
}

说说原理

swift在类中,类变量是能够保证线程安全,swift底层,static关键字的实际上是使用dispatch_once语法来实现的,如下一段swift编译中间产物SIL语言中的代码就能看到底层的实现.

1.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单例变量;从而保证了单例的不可修改特性。如果你要强行修改,编译器就会警告你,具体示例如下

到此这篇关于swift中正确安全声明一个单例的文章就介绍到这了,更多相关swift正确安全声明单例内容请搜索呐喊教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持呐喊教程!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。