不兼容的指针类型将'int'发送到'va

编程入门 行业动态 更新时间:2024-10-26 20:24:43
不兼容的指针类型将'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]]; }

更多推荐

本文发布于:2023-04-28 01:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329220.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   指针   不兼容   类型   va

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!