我正在尝试将NSInteger转换为NSString以在UIAlerView中显示NSInteger值。但问题是我得到这个错误:“当我试图转换NSInteger时,错误的接收器类型'NSInteger'(又名'long')”。
NSInteger:
NSInteger clasesDadas = clases.count;
clases.count是我在TableView中拥有的行数。
代码是:
-(void)infoAction
{
NSInteger clasesDadas = clases.count;
NSString *inStr = [NSString stringWithFormat:@"%d", [clasesDadas intValue]]; /*HERE IS THE ERROR*/
UIAlertView *alertatiempo = [[UIAlertView alloc] initWithTitle:@"Información" message:inStr delegate:self cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
[alertatiempo show];
}
任何人都可以帮助我吗?非常感谢你。
答案 0 :(得分:10)
intValue
不是NSInteger
上NSNumber
上存在的方法。
由于NSInteger
是32Bit上的基本类型int
和64位上的long
的typedef,因此它没有方法。
以下是您可以如何做到这一点,以确保它可以在32位和64位设备上运行。
NSString *inStr = [NSString stringWithFormat:@"%ld", (long)clasesDadas ];
同样如meronix所述,count方法会生成NSUInteger
,因此您可能需要相应地检查类型和格式
答案 1 :(得分:7)
您可以使用Boxed Expressions。
例如@(10).stringValue
或@(variable).stringValue
答案 2 :(得分:2)
您的clasesDadas已经是NSInteger
,因此您无法使用intValue
方法。
要将整数转换为字符串,您可以将其转换为NSNumber
并使用方法stringValue
,如下所示:
NSString *inStr = [[NSNumber numberWithInteger:clasesDadas] stringValue]
您还可以使用语法@()
将NSInteger
转换为对象(NSNumber)。例如:
NSString *inStr = [NSString stringWithFormat:@"%@", @(clasesDadas)];
答案 3 :(得分:1)
试试这个:
NSString *inStr = [NSString stringWithFormat:@"%lu", (unsigned long)clasesDadas];
NSInteger声明在32位或64位设备上可能有所不同,因此这是获取NSString的array.count的苹果建议
答案 4 :(得分:0)
NSString *inStr = [NSString stringWithFormat:@"%d", clasesDadas];
答案 5 :(得分:0)
NSString *inStr = [NSString stringWithFormat:@"%d", (int)clasesDadas];
这应该可以正常工作。
答案 6 :(得分:0)
NSString *inStr = [NSString stringWithFormat:@"%d", (int)clases.count];