Format指定类型'int'但参数的类型为'float'

时间:2016-01-07 06:04:18

标签: ios objective-c

如果我将常量定义为整数,它如何被识别为浮点数?

我在Xcode 6.1.1下构建并运行下面的代码没有任何问题。问题只出现在我升级到Xcode 7.2(7C68)和El Capitan之后。

iOSCircleView.m

int   const SECTORS_80 = 80;


@implementation iOSCircleView

- (void)ViewSettings_OverView       {

    sectors = SECTORS_80;

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

当我运行它时,NSLog会报告一个警告

  

格式指定类型'int'但参数的类型为'float'

控制台显示

  

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

如果我将NSLog命令更改为

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

并重新运行代码,没有警告,但控制台显示

  

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

这似乎是一个错误。但也许我错过了一些东西

以下是原始代码

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" ??

} 

正在运行(注意:问题发生在此之前)

    // run

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

这是完整的课程(未编辑)

     // 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];

}

这是.h文件

    #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

......问题变得立竿见影。我宣布扇区是浮动的,不知怎的,我之前是如何逃避这一点的。我的大脑还在度假!

1 个答案:

答案 0 :(得分:3)

您的问题与SECTORS_80无关。这是sectors变量的全部内容,因为这是您要记录的变量。您没有指定它的声明方式,但很明显错误表明您将其声明为float

变化:

float sectors;

为:

int sectors;

或更改格式以使用%f。无论哪种方式都将删除错误。但根据您的需要,只有一个是合适的。