You do not need to install it on nil . It is assumed that it will be built from an auto-implemented pointer (provided that it was built correctly, it will be freed). At the same time, do not hold onto it for the current stack stack. AutoreleasingUnsafeMutablePointer does not keep the object alive. When the closed pool of autoresists is opened, the wrapped object will be freed and probably freed. Like the name says: it is unsafe.
Avoid problems by never creating AutoreleasingUnsafeMutablePointer yourself in Swift ( edit ), unless it really is a UnsafeMutablePointer error and importing the C header made a mistake, see below). If you use it correctly, it should be a transparent glue between the Swift inout and the Objective-C return-by-pointer parameter.
Usually you create a var that matches the contained type and pass it to inout .
eg. if you want to call a function:
func someFunction(obj: AutoreleasingUnsafeMutablePointer<AnyObject?>)
then you call it like this:
var myObject: AnyObject? = nil someFunction(&AnyObject)
and everything will work.
I do not know of any other situation where you should keep AutoreleasingUnsafeMutablePointer . I donβt think you should manually create it on the Swift side other than nil . In Swift, itβs hard to create an AutoreleasingUnsafeMutablePointer construct with nil content, since the only way to auto- Unmanaged is to use Unmanaged .
Responding to your update ...
The functional signature of objc_getClassList is a failure in the automatic import of C. It incorrectly assumes that the Class * parameter should be imported as AutoreleasingUnsafeMutablePointer<AnyObject?> . You really need an UnsafeMutablePointer , which you can get from the array:
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)) }