如何解决 Swift 5.2 中的“Inout 表达式创建临时指针”问题?

Coc*_*ser 1 swift5

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

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:)'
Run Code Online (Sandbox Code Playgroud)

fis*_*her 5

参考此讨论;

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!
    )
}
Run Code Online (Sandbox Code Playgroud)