swift 单例模式的原理及应用

129 阅读1分钟
public class FileManager {

    public static let shared = FileManager()
    
    // 闭包表达式
    
	//    public static let shared = {

	//        return FileManager()

	//    }()

    private init() {}
    
    func open() {
        
    }
    
    func close() {
        
    }
}

1、 static 修饰的内存是属于全局区内存,相当于全局变量。

2、 通过调试汇编可以发现static 最终调用的是 swift_once -> dispatch_once_f, GCD底层的方法。

3、 单例类和实例根据需求使用public修饰。