简体   繁体   English

CGFloat或CGFloat *?

[英]CGFloat or CGFloat *?

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 . 错误指向actualFontSize:myFont.pointSize myFont is a UIFont . myFontUIFont I set it like so: UIFont *myFont = [UIFont systemFontOfSize:17.0]; 我这样设置: UIFont *myFont = [UIFont systemFontOfSize:17.0];

What does that error mean and any ideas on how to fix it? 该错误是什么意思,以及有关如何解决该错误的想法?

From the docs : 文档

actualFontSize On input, a pointer to a floating-point value. actualFontSize输入时,指向浮点值的指针。 On return, this value contains the actual font size that was used to render the string. 返回时,此值包含用于呈现字符串的实际字体大小。

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. drawAtPoint:将设置outSize指向的值,以便您可以打印结果。 The reason for this here is, that you can only return 1 value in C via return statement. 这样做的原因是,您只能通过return语句在C中返回1值。 To get multiple results, you can pass in pointers to allow the method to set additional results. 要获得多个结果,可以传入指针以允许该方法设置其他结果。

The actualFontSize parameter 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. 实际的actualFontSize参数应该是您传入的CGFloat的地址。如果该方法使用不同的字体大小,它将更改您传入的值,这就是为什么您需要传递该地址而不是该值的原因。 In other words, you're passing actualFontSize by reference so that the method can update it as necessary. 换句话说,您通过引用传递了actualFontSize ,以便该方法可以根据需要对其进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM