This
let str : NSString = ""
let pointer = UnsafeMutablePointer(mutating: str.utf8String)
is already undefined behavior: You are creating a mutable pointer referencing the same memory as the (immutable) str.utf8String
.
Actually it is much simpler: You can pass the address of an optional NSString
as inout argument with &
.
var param: NSString?
let result = YourObjCClass.funcName(¶m)
The compiler automatically does the necessary conversion. There is seldom a need to create an AutoreleasingUnsafeMutablePointer
explicitly.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…