Swift error: '&' is used with a non-inout argument of type UnsafeMutablePointer '

I am trying to convert the following Objective-C code ( source ) from this

-(CGRect) dimensionsForAttributedString: (NSAttributedString *) asp {

    CGFloat ascent = 0, descent = 0, width = 0;
    CTLineRef line = CTLineCreateWithAttributedString( (CFAttributedStringRef) asp);

    width = CTLineGetTypographicBounds( line, &ascent, &descent, NULL );

    // ...
}

in Swift:

func dimensionsForAttributedString(asp: NSAttributedString) -> CGRect {

    let ascent: CGFloat = 0
    let descent: CGFloat = 0
    var width: CGFloat = 0
    let line: CTLineRef = CTLineCreateWithAttributedString(asp)

    width = CTLineGetTypographicBounds(line, &ascent, &descent, nil)

    // ...
}

But I get an error with &ascentin this line:

width = CTLineGetTypographicBounds(line, &ascent, &descent, nil)

'&' is used with a non-inout argument of type UnsafeMutablePointer

Xcode suggests fixing the error by deleting &. When I do this, I get an error

Cannot convert value of type 'CGFloat' to the expected argument type 'UnsafeMutablePointer'

Interacting with API documentation The C API uses syntax &, so I don’t understand what the problem is. How to fix this error?

+4
1

ascent descent &:

var ascent: CGFloat = 0
var descent: CGFloat = 0
let line: CTLineRef = CTLineCreateWithAttributedString(asp)
let width = CGFloat(CTLineGetTypographicBounds(line, &ascent, &descent, nil))

CTLineGetTypographicBounds() . , a Double, CGFloat.

+5

Source: https://habr.com/ru/post/1619612/


All Articles