如何解决 Swift 5.2 中的 "Inout expression creates a temporary pointer" 问题?

How do I resolve "Inout expression creates a temporary pointer" issue in Swift 5.2?

我在 swift 5.2 中调用 CTParagraphStyleSetting 函数时如何解决这个问题?

function myFunction() {
    var alignment: CTTextAlignment = .left
    var settings = CTParagraphStyleSetting(spec: .alignment, 
                                           valueSize: 1, 
                                           value: &alignment)
}

issue:
Inout expression creates a temporary pointer, but argument 'value' should 
be a pointer that outlives the call to 'init(spec:valueSize:value:)'

参考这个讨论;

https://forums.swift.org/t/swift-5-2-pointers-and-coretext/34862,

你可以这样写:

let alignment: CTTextAlignment = .left
let settings: CTParagraphStyleSetting = withUnsafeBytes(of: alignment) { alignment in
    CTParagraphStyleSetting(
        spec: .alignment, 
        valueSize: 1, 
        value: alignment.baseAddress!
    )
}