Daniel Fernandez Daniel Fernandez - 1 year ago 201
Objective-C Question

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. But the problem is that I get this error: "Bad receiver type 'NSInteger' (aka 'long')" when I'm trying to convert the NSInteger.

The NSInteger:

NSInteger clasesDadas = clases.count;

The clases.count is the number of rows I have in the TableView.

The code is:

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.

Answer Source

intValue is not methods that exist on a NSInteger but on NSNumber. Since NSInteger is a typedef for a primitive type int on 32Bit and long on 64bit it does not have an methods.

Here is how you'd could do it, casting to make sure it works on both 32 and 64 bit devices.

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download