1

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

我的应用程序中有这一行:

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

那条线来自第 3 方库,所以我试图摆脱警告并使其正常工作。无论如何,我将如何解决此警告?

谢谢!

完整方法:

- (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);
}
4

1 回答 1

4

为什么不改成:

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

这假设arg具有类型int

您只会使用initWithFormat:arguments:if argis 实际上是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]];
}
于 2013-04-06T02:30:27.263 回答