Greg
Greg

Reputation: 1842

Format specifies type ‘int’ but the argument has type ‘float’

If I define a constant as an integer, how is it recognised as a float ?

I built and ran the code below without problems under Xcode 6.1.1. The problem only appeared after I upgraded to Xcode 7.2 (7C68) and El Capitan.

iOSCircleView.m

int   const SECTORS_80 = 80;


@implementation iOSCircleView

- (void)ViewSettings_OverView       {

    sectors = SECTORS_80;

        NSLog(@"sectors %i", sectors);
  } 

When I run it NSLog reports a warning

Format specifies type ‘int’ but the argument has type ‘float’

The console displays

2016-01-07 10:05:00.210 AutoDrawUI[1298:555384] sectors 855670784

If I change NSLog command to

    NSLog(@"sectors %f”, sectors);

and rerun the code, there is no warning but the console displays

2016-01-07 10:38:01.547 AutoDrawUI[1330:632806] sectors 80.000000

This seems like a bug. But maybe I’ve missed something

Here is the Original Code

int   const SECTORS_80          = 80;

@implementation iOSCircleView

// Initialisation

// frame a view for drawing

- (id)initWithFrame:(CGRect)frame   {
    self = [super initWithFrame:frame];
    if (self) {

        // Initialization code
        totalCircles            = [[NSMutableArray alloc] init];
        centre                  = [[NSMutableArray alloc] init];
        ring                    = [[NSMutableArray alloc] init];

        // Set background color
        self.backgroundColor    = [UIColor grayColor];

    }
    return self;
}

    // initialise single target view (7 others are commented out)

- (void)drawRect:(CGRect)rect       {

    [self ViewSettings_OverView];           // 1. "launch view" ??

} 

Running (note: the problem occurs before it gets here)

    // run

- (void)autoDrawUI                  {
        [self drawCircle];
}   

Here is the full class (unedited)

     // initialise display settings

- (void)ViewSettings_OverView       {

    viewState = 0;
    // number of dots on perimeter
    sectors         = SECTORS_80;             // WARNING HERE
    limit           = sectors;;

    if (sectors <= 0) {                       // WARNING HERE

        show        = FALSE;                  // disable
    } else
        show        = TRUE;                   // enable perimeter dots

    // radius of large circle perimeter
    uberRadius      = UBERRADIUS_52;

    // radius of dots on large circle perimeter
    dotRadius       = DOTRADIUS_4;

    dotsFilled      = TRUE;                // outline
    oneColour       = TRUE;                // every colour
    alternateDots   = TRUE;                // alternately filled and coloured

    oddEvenState    = 1;                    // PlayView

    ringDot         = RINGDOT_HIDE;       //_SHOW104
    centreDotFilled = FALSE;                // fill or outline

    centreDot       = CENTREDOT_HIDE;       //CENTREDOT_SHOW26;
    centreDotFilled = TRUE;                 // fill or outline

        NSLog(@"View %i", viewState);
        NSLog(@"sectors %i", sectors);      // WARNING HERE
        NSLog(@"show %i", show);
    [self centreReference];

}

And here is the .h file

    #import <UIKit/UIKit.h>

@interface iOSCircleView : UIView
{
    NSMutableArray  *totalCircles;
    NSMutableArray  *sectorDots;

    //                                      following Stackoverflow 08/02/2015 ?
    NSMutableArray  *centre;                // 1 of 5 (Global View), centre (Wait View), even bell icon (Play View)
    NSMutableArray  *ring;                  // odd bell icon (Play View)
                                            //      TO DO
    NSMutableArray  *sectorDotsCyan;        // 16 moons surround 1 of 5 buttons on Global View
    NSMutableArray  *sectorDotsRed;         // 16 moons          2
    NSMutableArray  *sectorDotsYellow;      // 16 moons          3
    NSMutableArray  *sectorDotsMagenta;     // 16 moons          4
    NSMutableArray  *sectorDotsGreen;       // 16 moons          5

    int             dotCount,               // working
                    colourIndex,            // working
                    toggleIndex,            // working
                    imageIndex,
                    limit,                  // working
                    selectedZone,           // working
                    selectedPhone,
                    selectedState,
                    state,
                    viewState,              // working
                    ringDotFilled,
                    centreDotFilled,        // working
                    dotsFilled,             // working
                    labelShown,
                    oneColour,              // working
                    alternateDots,
                    i,                      // working
                    show;                   // working

    BOOL            shakeTap,               // PlayView
                    oddEvenPhone,           // LocalView
                    oddEvenState;           // working

    float           uberX,                  // working
                    uberY,                  // working
                    uberRadius,             // working
                    uberAngle,              // working
                    labelX,
                    labelY,
                    dotRadius,              // working
                    sectors,                // working
                    x,                      // working
                    y,                      // working
                    ringDot,                // working
                    centreDot,              // working
                    textOffset,             // working
                    startAngle,
                    dotAngle,
                    endAngle,
                    angle;

    CGPoint         dotPosition;
    CGRect          boxBoundary;
    CGContextRef    context;
}

@property(nonatomic,retain) UIColor* fillColor;
@property(nonatomic,retain) UIColor* circle;
@property(nonatomic,retain) UIImage* anImage;


- (void)drawCircle;
- (void)drawRosette;

@end

... and the problem becomes immediately obvious. I declared sectors as float and somehow how I got away with this previously. My brain's still on holiday!

Upvotes: 0

Views: 1401

Answers (1)

rmaddy
rmaddy

Reputation: 318824

Your issue has nothing to do with SECTORS_80. It's all about the sectors variable since that is the variable you are logging. You didn't specify how it is declared but clearly the error indicates that you declared it as a float.

Change:

float sectors;

to:

int sectors;

Or change the format to use %f. Either way will remove the error. But only one is really appropriate depending on your needs.

Upvotes: 3

Related Questions