Swift error: '&' used with non-inout argument of type 'UnsafeMutablePointer'
Swift error: '&' used with non-inout argument of type 'UnsafeMutablePointer'
我正在尝试从这个
转换以下Objective-C代码(source)
-(CGRect) dimensionsForAttributedString: (NSAttributedString *) asp {
CGFloat ascent = 0, descent = 0, width = 0;
CTLineRef line = CTLineCreateWithAttributedString( (CFAttributedStringRef) asp);
width = CTLineGetTypographicBounds( line, &ascent, &descent, NULL );
// ...
}
进入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)
// ...
}
但是我在这一行中收到 &ascent
的错误:
width = CTLineGetTypographicBounds(line, &ascent, &descent, nil)
'&' used with non-inout argument of type 'UnsafeMutablePointer'
Xcode 建议我通过删除 &
来修复它。但是,当我这样做时,我得到了错误
Cannot convert value of type 'CGFloat' to expected argument type 'UnsafeMutablePointer'
Interacting with C APIs documentation 使用 &
语法,所以我看不出问题是什么。我该如何解决这个错误?
ascent
和descent
必须是变量才能被传递
作为输入输出参数 &
:
var ascent: CGFloat = 0
var descent: CGFloat = 0
let line: CTLineRef = CTLineCreateWithAttributedString(asp)
let width = CGFloat(CTLineGetTypographicBounds(line, &ascent, &descent, nil))
从 CTLineGetTypographicBounds()
开始的 return,这些变量将被设置为
线的上升和下降。还要注意这个函数 returns
Double
,因此您需要将其转换为 CGFloat
。
我正在尝试从这个
转换以下Objective-C代码(source)-(CGRect) dimensionsForAttributedString: (NSAttributedString *) asp {
CGFloat ascent = 0, descent = 0, width = 0;
CTLineRef line = CTLineCreateWithAttributedString( (CFAttributedStringRef) asp);
width = CTLineGetTypographicBounds( line, &ascent, &descent, NULL );
// ...
}
进入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)
// ...
}
但是我在这一行中收到 &ascent
的错误:
width = CTLineGetTypographicBounds(line, &ascent, &descent, nil)
'&' used with non-inout argument of type 'UnsafeMutablePointer'
Xcode 建议我通过删除 &
来修复它。但是,当我这样做时,我得到了错误
Cannot convert value of type 'CGFloat' to expected argument type 'UnsafeMutablePointer'
Interacting with C APIs documentation 使用 &
语法,所以我看不出问题是什么。我该如何解决这个错误?
ascent
和descent
必须是变量才能被传递
作为输入输出参数 &
:
var ascent: CGFloat = 0
var descent: CGFloat = 0
let line: CTLineRef = CTLineCreateWithAttributedString(asp)
let width = CGFloat(CTLineGetTypographicBounds(line, &ascent, &descent, nil))
从 CTLineGetTypographicBounds()
开始的 return,这些变量将被设置为
线的上升和下降。还要注意这个函数 returns
Double
,因此您需要将其转换为 CGFloat
。