简体   繁体   English

将NSInteger转换为NSString Xcode iOS8

[英]Convert a NSInteger to NSString Xcode iOS8

I'm trying to convert an NSInteger to a NSString to show the NSInteger value in a UIAlerView. 我正在尝试将NSInteger转换为NSString以在UIAlerView中显示NSInteger值。 But the problem is that I get this error: "Bad receiver type 'NSInteger' (aka 'long')" when I'm trying to convert the NSInteger. 但问题是我得到这个错误:“当我试图转换NSInteger时,错误的接收器类型'NSInteger'(又名'long')”。

The NSInteger: NSInteger:

NSInteger clasesDadas = clases.count;

The clases.count is the number of rows I have in the TableView. clases.count是TableView中的行数。

The code is: 代码是:

-(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];

}

Can anyone help me? 谁能帮我? Thank you very much. 非常感谢你。

intValue is not methods that exist on a NSInteger but on NSNumber . intValue不是NSInteger上存在但NSNumber上存在的方法。 Since NSInteger is a typedef for a primitive type int on 32Bit and long on 64bit it does not have an methods. 由于NSInteger是32Bit上的基本类型int的typedef,而在64bit上是long ,因此它没有方法。

Here is how you'd could do it, casting to make sure it works on both 32 and 64 bit devices. 以下是您可以做到的方法,以确保它可以在32位和64位设备上运行。

NSString *inStr = [NSString stringWithFormat:@"%ld", (long)clasesDadas ]; 

Also as stated by meronix the count method will result in an NSUInteger so you might want to check the type and format accordingly 同样如meronix所述,count方法将导致NSUInteger因此您可能需要相应地检查类型和格式

You can use with Boxed Expressions. 您可以使用Boxed表达式。

Eg @(10).stringValue or @(variable).stringValue 例如@(10).stringValue@(variable).stringValue

Your clasesDadas is already a NSInteger , so you cannot use the method intValue . 你的clasesDadas已经是一个NSInteger ,所以你不能使用intValue方法。

To convert your integer to string you can convert it in NSNumber and use the method stringValue like this : 要将整数转换为字符串,可以将其转换为NSNumber并使用方法stringValue如下所示:

NSString *inStr = [[NSNumber numberWithInteger:clasesDadas] stringValue]

You can also use the syntax @() to convert your NSInteger to object (NSNumber). 您还可以使用语法@()NSInteger转换为对象(NSNumber)。 For example : 例如 :

NSString *inStr = [NSString stringWithFormat:@"%@", @(clasesDadas)];

try this: 尝试这个:

NSString *inStr = [NSString stringWithFormat:@"%lu", (unsigned long)clasesDadas];

NSInteger declaration may differ on 32 or 64 bit devices, so this is the apple suggestion to get an array.count for a NSString 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM