简体   繁体   中英

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 . myFont is a UIFont . I set it like so: 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. 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. 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.

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. In other words, you're passing actualFontSize by reference so that the method can update it as necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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