不兼容的指针类型将'int'发送到'va_list'类型的参数(又名'char')(Incompatible pointer types sending 'int' to parameter of type 'va_list' (aka 'char'))
我有一个简单但令人沮丧的问题。
我的应用程序中有这一行:
[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); }I have a simple yet frustrating problem.
I have this line in my app:
[super setText:[[[NSString alloc] initWithFormat:@"%i" arguments:arg] autorelease]];That line was from a 3rd party library so I trying to just get rid of the warning and make it work properly. Anyway how would I fix this warning?
Thanks!
Full method:
- (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); }最满意答案
为什么不将其更改为:
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]]; }Why not change it to:
NSString *text = [NSString stringWithFormat:@"%i", arg]; [super setText:text];This assumes that arg has a type of int.
You would only use initWithFormat:arguments: if arg is actually is a va_list from a variable argument list.
Update: Based on the updated code you posted you can do:
- (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]]; }更多推荐
发布评论