xcode CGFloat 或 CGFloat *?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11109984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:38:45  来源:igfitidea点击:

CGFloat or CGFloat *?

objective-ciosxcodecocoa-touchuifont

提问by Dvir Levy

I am getting an error that I don't understand how to fix. The error is:

我收到一个错误,我不知道如何解决。错误是:

Sending 'CGFloat' (aka 'float') to parameter of incompatible type 'CGFloat *' (aka 'float *'); 

For line:

对于线:

[xlabel.text drawAtPoint:CGPointMake(labelRect.origin.x, labelRect.origin.y) 
                forWidth:labelRect.size.width 
                withFont:myFont 
             minFontSize:5.0 
          actualFontSize:myFont.pointSize 
           lineBreakMode:UILineBreakModeWordWrap 
      baselineAdjustment:UIBaselineAdjustmentAlignCenters];

the error points to actualFontSize:myFont.pointSize. myFontis a UIFont. I set it like so: UIFont *myFont = [UIFont systemFontOfSize:17.0];

错误指向actualFontSize:myFont.pointSize. myFont是一个UIFont。我是这样设置的:UIFont *myFont = [UIFont systemFontOfSize:17.0];

What does that error mean and any ideas on how to fix it?

该错误是什么意思以及如何修复它的任何想法?

回答by Thomas Zoechling

From the docs:

文档

actualFontSize On input, a pointer to a floating-point value. On return, this value contains the actual font size that was used to render the string.

actualFontSize 输入时,指向浮点值的指针。返回时,该值包含用于呈现字符串的实际字体大小。

Which means that you have to pass in a pointer, by referencing a variable. To do so you use the ampersand operator &

这意味着您必须通过引用变量来传递指针。为此,您可以使用与号运算符&

Example:

例子:

CGFloat outSize = 0;
[xlabel.text drawAtPoint:CGPointMake(labelRect.origin.x, labelRect.origin.y) 
                forWidth:labelRect.size.width 
                withFont:myFont 
             minFontSize:5.0 
          actualFontSize:&outSize 
           lineBreakMode:UILineBreakModeWordWrap 
      baselineAdjustment:UIBaselineAdjustmentAlignCenters];
NSLog(@"%f", outSize);

drawAtPoint: will set the value that outSize points to and so you can print the result. The reason for this here is, that you can only return 1 value in C via return statement. To get multiple results, you can pass in pointers to allow the method to set additional results.

drawAtPoint: 将设置 outSize 指向的值,以便您可以打印结果。这样做的原因是,您只能通过 return 语句在 C 中返回 1 个值。要获得多个结果,您可以传入指针以允许该方法设置其他结果。

回答by Caleb

The actualFontSizeparameter is supposed to be the address of a CGFloat that you pass in. If the method uses a different font size, it'll change the value that you pass in, which is why you need to pass the address instead of the value. In other words, you're passing actualFontSizeby reference so that the method can update it as necessary.

actualFontSize参数应该是您传入的 CGFloat 的地址。如果该方法使用不同的字体大小,它将更改您传入的值,这就是您需要传递地址而不是值的原因。换句话说,您actualFontSize通过引用传递,以便方法可以根据需要更新它。