Dvir Levy
Dvir Levy

Reputation: 8128

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?

Upvotes: 1

Views: 10285

Answers (2)

Thomas Zoechling
Thomas Zoechling

Reputation: 34263

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.

Upvotes: 7

Caleb
Caleb

Reputation: 125017

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.

Upvotes: 3

Related Questions