[英]Convert a NSInteger to NSString Xcode iOS8
我正在尝试将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];
}
谁能帮我? 非常感谢你。
您可以使用Boxed表达式。
例如@(10).stringValue
或@(variable).stringValue
你的clasesDadas已经是一个NSInteger
,所以你不能使用intValue
方法。
要将整数转换为字符串,可以将其转换为NSNumber
并使用方法stringValue
如下所示:
NSString *inStr = [[NSNumber numberWithInteger:clasesDadas] stringValue]
您还可以使用语法@()
将NSInteger
转换为对象(NSNumber)。 例如 :
NSString *inStr = [NSString stringWithFormat:@"%@", @(clasesDadas)];
尝试这个:
NSString *inStr = [NSString stringWithFormat:@"%lu", (unsigned long)clasesDadas];
NSInteger声明可能在32位或64位设备上有所不同,因此这是获得NSString的array.count的苹果建议
NSString *inStr = [NSString stringWithFormat:@"%d", clasesDadas];
NSString *inStr = [NSString stringWithFormat:@"%d", (int)clasesDadas];
这应该工作正常。
NSString *inStr = [NSString stringWithFormat:@"%d", (int)clases.count];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.