class ZRSingleton {
private let concurrentQueue = DispatchQueue(label: "ConcurrentQueue", attributes: .concurrent, target: nil)
private var queueDic = [String: String]()
static let sharedInstance = ZRSingleton()
private init(){
}
func setDicValue(value: String, for key: String){
concurrentQueue.async(flags: .barrier) {
self.queueDic[key] = value
}
}
func getDicValue(for key: String) -> String? {
var result: String?
concurrentQueue.sync {
result = queueDic[key]
}
return result
}
}
vc
func createPattern() {
let single = ZRSingleton.sharedInstance;
let single2 = ZRSingleton.sharedInstance;
single.setDicValue(value: "11111", for: "Z")
print(single.getDicValue(for: "Z") ?? "nil")
//打印地址
print("address:\(Unmanaged.passUnretained(single).toOpaque() )")
print("address:\(Unmanaged.passUnretained(single2).toOpaque() )")
}
网友评论