Search code examples
swift5

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


How do i resolve this issue when i calling CTParagraphStyleSetting function in 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:)'

Solution

  • In reference to this discussion;

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

    you can write it like this:

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