3

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:

-(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.

KPM
  • 10,558
  • 3
  • 45
  • 66
Daniel Fernandez
  • 241
  • 3
  • 6
  • 14
  • possible duplicate of [How do I convert NSInteger to NSString datatype?](http://stackoverflow.com/questions/1796390/how-do-i-convert-nsinteger-to-nsstring-datatype) – picciano Mar 26 '15 at 13:33

7 Answers7

12

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

Community
  • 1
  • 1
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Not it is not since the `NSInteger` is signed casting to to unsigned be yield errors. But in a count you better use `NSUInteger `. – rckoenes Mar 26 '15 at 13:28
7

You can use with Boxed Expressions.

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

Min Soe
  • 1,214
  • 1
  • 14
  • 25
2

Your clasesDadas is already a NSInteger, so you cannot use the method intValue.

To convert your integer to string you can convert it in NSNumber and use the method stringValue like this :

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

You can also use the syntax @() to convert your NSInteger to object (NSNumber). For example :

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

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

meronix
  • 6,175
  • 1
  • 23
  • 36
  • Why are you casting it from signed to unsigned ? In know the count returns a `NSUInteger` but just casting cab yield in unexpected results. – rckoenes Mar 26 '15 at 13:28
  • i was assuming that "count" was NSUInteger, as an array.count, as he said it's "the number of rows I have in the TableView" (NSUInteger) – meronix Mar 26 '15 at 13:32
  • yes but then you should also advice to declare `clasesDadas` as an `NSUInteger` – rckoenes Mar 26 '15 at 13:33
  • 1
    right, you changed your comment, as i said: "also", now i agree with you – meronix Mar 26 '15 at 13:37
0
NSString *inStr = [NSString stringWithFormat:@"%d", clasesDadas];
vichevstefan
  • 898
  • 7
  • 16
0
NSString *inStr = [NSString stringWithFormat:@"%d", (int)clasesDadas];

This should work fine.

Peter Lendvay
  • 565
  • 4
  • 22
0
 NSString *inStr = [NSString stringWithFormat:@"%d", (int)clases.count]; 
Guru
  • 21,652
  • 10
  • 63
  • 102