使用UnsafePointer或UnsafeMutablePointer,我需要显式管理其内存:
let buffer = sizeof(Int8) * 4 var ptr = UnsafeMutablePointer<Void>.alloc(buffer) defer { ptr.destroy() ptr.dealloc(someVal) ptr = nil }
但是对于AutoreleasingUnsafeMutablePointer对象,文档是不明确的.我无法在AutoreleasingUnsafeMutablePointer上显式调用destroy或dealloc.
var ptr: AutoreleasingUnsafeMutablePointer<Void> = nil defer { ptr = nil } // assign something to ptr
该名称暗示它在超出范围后自动释放,但是我是否需要将AutoreleasingUnsafeMutablePointer设置为nil才能自动释放?
这是一个示例,我使用AutoreleasingUnsafeMutablePointer来获取运行时当前加载的所有类的列表.请注意,在调用Objective-C运行时的功能时,某些函数需要AutoreleasingUnsafeMutablePointer而不仅仅是UnsafeMutablePointer:
var numClasses: Int32 = 0 var allClasses: AutoreleasingUnsafeMutablePointer<AnyClass?> = nil defer { allClasses = nil // is this required? } numClasses = objc_getClassList(nil,0) if numClasses > 0 { var ptr = UnsafeMutablePointer<AnyClass>.alloc(Int(numClasses)) defer { ptr.destroy() ptr.dealloc(Int(numClasses)) ptr = nil } allClasses = AutoreleasingUnsafeMutablePointer<AnyClass?>.init(ptr) numClasses = objc_getClassList(allClasses,numClasses) for i in 0 ..< numClasses { if let currentClass: AnyClass = allClasses[Int(i)] { print("\(currentClass)") } } }
通过永远不在Swift中自己创建AutoreleasingUnsafeMutablePointer来避免问题(编辑:除非它真的是一个UnsafeMutablePointer并且C头导入出错了,见下文).如果你正确使用它,它应该是Swift的inout和Objective-C返回指针参数之间的透明胶水.
您通常会创建一个与所包含类型匹配的var,并将其传入inout.
func someFunction(obj: AutoreleasingUnsafeMutablePointer<AnyObject?>)
然后你像这样调用它:
var myObject: AnyObject? = nil someFunction(&AnyObject)
一切都会好起来的.
我不知道你应该持有AutoreleasingUnsafeMutablePointer的任何其他情况.我不认为你应该在Swift方面手动构建一个,除非是nil.在Swift中构建具有非零内容的AutoreleasingUnsafeMutablePointer是非常困难的,因为自动释放的唯一方法是使用Unmanaged.
回应您的更新……
objc_getClassList函数签名是Swift自动C导入中的一个小故障.它错误地假设应该将Class *参数导入为AutoreleasingUnsafeMutablePointer< AnyObject?>.你真的只需要一个可以从数组中获取的UnsafeMutablePointer:
var allClasses = Array<AnyClass?>(count: Int(objc_getClassList(nil,0)),repeatedValue: nil) allClasses.withUnsafeMutableBufferPointer { (inout bp: UnsafeMutableBufferPointer<AnyClass?>) in objc_getClassList(AutoreleasingUnsafeMutablePointer(bp.baseAddress),Int32(allClasses.count)) }