不兼容的指针类型将'int'发送到'va_list'类型的参数(又名'char')

时间:2013-04-06 02:23:34

标签: ios nsstring char arguments

我有一个简单但令人沮丧的问题。

我的应用中有这一行:

[super setText:[[[NSString alloc] initWithFormat:@"%i" arguments:arg] autorelease]];

该行来自第三方库,所以我试图摆脱警告并使其正常工作。无论如何,我如何修复此警告?

谢谢!

完整方法:

- (void)timerLoop:(NSTimer *)aTimer {
    //update current value
    currentTextNumber += currentStep;

    //check if the timer needs to be disabled
    if ( (currentStep >= 0 && currentTextNumber >= textNumber) || (currentStep < 0 && currentTextNumber <= textNumber) ) {
        currentTextNumber = textNumber;
        [self.timer invalidate];
    }

    //update the label using the specified format
    int value = (int)currentTextNumber;
    int *arg = (int *)malloc(sizeof(int));
    memcpy(arg, &value, sizeof(int));
    //call the superclass to show the appropriate text
    [super setText:[[[NSString alloc] initWithFormat:@"%i" arguments:arg] autorelease]];
    free(arg);
}

1 个答案:

答案 0 :(得分:4)

为什么不将其更改为:

NSString *text = [NSString stringWithFormat:@"%i", arg];
[super setText:text];

这假定arg的类型为int

如果initWithFormat:arguments:实际上是变量参数列表中的arg,则只能使用va_list

更新:根据您发布的更新代码,您可以执行以下操作:

- (void)timerLoop:(NSTimer *)aTimer {
    //update current value
    currentTextNumber += currentStep;

    //check if the timer needs to be disabled
    if ( (currentStep >= 0 && currentTextNumber >= textNumber) || (currentStep < 0 && currentTextNumber <= textNumber) ) {
        currentTextNumber = textNumber;
        [self.timer invalidate];
    }

    //update the label using the specified format
    int value = (int)currentTextNumber;
    [super setText:[NSString stringWithFormat:@"%i", value]];
}