本文主要是介绍【iOS/Swift】内存引用计数学习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
内存地址获取代码
func printAddress(values:AnyObject...){for value in values {print(Unmanaged.passUnretained(value).toOpaque())}print("+++++++++++++++++")
}
打印指针地址
- 方法一
let tt = {}
withUnsafePointer(to: &tt) {print($0)}
- 方法二
func test(a: UnsafePointer<String>) {let b = UnsafeRawPointer(a)print("pointer::\(a)")let t = b.load(as: String.self)print("value::" + t)
}
- 方法三
func address(o: UnsafeRawPointer) -> String {return String.init(format: "%018p", Int(bitPattern: o))
}
引用计数探索
class Student: NSObject {var name = "abc"func test() {let block = { [weak self] inguard let `self` = self else {return}self.name = "nnnn"self.name = "mmm"self.name = "mmm"print(CFGetRetainCount(self)) // 计数2}block()}deinit {print("deinit:\(CFGetRetainCount(self))")}
}func test() {let user1 = Student() // 计数1user1.test()
}test()
//第一种方式,KVC
[obj valueForKey:@"retainCount"] //第二种方式,runTime
OBJC_EXTERN int _objc_rootRetainCount(id);
_objc_rootRetainCount(obj) //第三种方式
CFGetRetainCount((__bridge CFTypeRef)(obj))
您的一举一动都是对我的莫大支持
- 指针等方法
友情链接
这篇关于【iOS/Swift】内存引用计数学习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!