137

So I have an NSArray "myArray" with NSNumbers and NSStrings. I need them in another UIView so i go like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

DetailViewController *details = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
details.subjectText = [[myArray objectAtIndex:indexPath.row] objectForKey:@"subject"];

The subjectText works. But how can I get the NSNumbers out of it? (I actually need them as strings...) I would convert a NSString out of a NSNumber like this: NSString *blah = [NSNumber intValue]. But I don't know how to set it up in the code above...

Hamed Rajabi Varamini
  • 3,439
  • 3
  • 24
  • 38
dav3
  • 1,447
  • 2
  • 10
  • 9
  • Your array seems actually to contain dictionaries, judging by the use of `objectForKey`. So you'll need to extract the appropriate values from that before attempting to convert. Note that most Cocoa object types, including NSNumber, can be converted to strings by calling the `description` method (or using `%@` in a format string). – walkytalky Oct 19 '10 at 16:04
  • wow.. thank you walkytalky! can you point me to a documentation or give a example code to comprehend it? =) – dav3 Oct 19 '10 at 16:26

7 Answers7

389

Try:

NSString *myString = [NSNumber stringValue];
JonLOo
  • 4,929
  • 1
  • 19
  • 27
  • 3
    but you should take the NSNumber which is inside the array and then call StringValue method, something like NSString *myString= [[myArray objectAtIndex:i] stringValue]; but you have to be sure that you have an NSNumber at that index – JonLOo Oct 19 '10 at 15:47
  • I prefer descriptionWithLocale:[NSLocale currentLocale] because it works both for ints and floats. – just.do.it Apr 11 '15 at 17:22
  • @JonLOo when using this for specific number the decimal values loose exact values. For Eg : NSNumber *num = [NSNumber numberWithDouble:48.3]; NSString *strNumber = num.stringValue; gives results as 48.299999; this becomes critical if its Amount and has to deal with in real world and multiplied with big numbers.!! Any thought? – Vish Aug 03 '15 at 06:13
  • hi @Vish, i haven't try this solution so im not sure if it will work, try formatting it with the `%f` like this: `[NSString stringWithFormat:@"%f", yourDouble];` – JonLOo Aug 31 '15 at 09:02
  • "No known class method for selector 'stringValue'" <-- stringValue is not a class function so you can't call it on NSNumber directly. rhalgravez's answer does it correctly. – jeremywhuff Dec 27 '17 at 17:11
14

You can do it with:

NSNumber *myNumber = @15;
NSString *myNumberInString = [myNumber stringValue];
Vivek Deore
  • 71
  • 10
rhalgravez
  • 366
  • 2
  • 9
7
//An example of implementation :
// we set the score of one player to a value
[Game getCurrent].scorePlayer1 = [NSNumber numberWithInteger:1];
// We copy the value in a NSNumber
NSNumber *aNumber = [Game getCurrent].scorePlayer1;
// Conversion of the NSNumber aNumber to a String with stringValue
NSString *StringScorePlayer1 = [aNumber stringValue];
fcoque
  • 71
  • 1
  • 1
5

or try NSString *string = [NSString stringWithFormat:@"%d", [NSNumber intValue], nil];

Tyilo
  • 28,998
  • 40
  • 113
  • 198
sbs
  • 4,102
  • 5
  • 40
  • 54
  • 8
    This does two conversions. One to turn NSNumber into intValue and one to coerce the resulting intValue into an NSString. Using this may be small to some, but removing it is a great way to save small computational cycles...which can add up. – Jann Jun 01 '11 at 22:56
  • This assumes that the numbers being stored are ints. What if you don't know the type of number stored? – James Webster Mar 04 '14 at 13:10
3

The funny thing is that NSNumber converts to string automatically if it becomes a part of a string. I don't think it is documented. Try these:

NSLog(@"My integer NSNumber:%@",[NSNumber numberWithInt:184]);
NSLog(@"My float NSNumber:%@",[NSNumber numberWithFloat:12.23f]);
NSLog(@"My bool(YES) NSNumber:%@",[NSNumber numberWithBool:YES]);
NSLog(@"My bool(NO) NSNumber:%@",[NSNumber numberWithBool:NO]);

NSString *myStringWithNumbers = [NSString stringWithFormat:@"Int:%@, Float:%@ Bool:%@",[NSNumber numberWithInt:132],[NSNumber numberWithFloat:-4.823f],[NSNumber numberWithBool:YES]];
NSLog(@"%@",myStringWithNumbers);

It will print:

My integer NSNumber:184
My float NSNumber:12.23
My bool(YES) NSNumber:1
My bool(NO) NSNumber:0
Int:132, Float:-4.823 Bool:1

Works on both Mac and iOS

This one does not work:

NSString *myNSNumber2 = [NSNumber numberWithFloat:-34512.23f];
Tibidabo
  • 21,461
  • 5
  • 90
  • 86
  • 2
    This is documented: the `%@` format specifier will send the `-description` message to the corresponding receiver object. You can make your last line work in the same spirit by writing: `NSString *myNSNumber2 = [[NSNumber numberWithFloat:-34512.23f] description];`. As far as I can tell, `NSNumber`'s `description` and `stringValue` methods do the same thing. – Jean-Denis Muys Mar 22 '16 at 08:08
2

In Swift you can do like this

let number : NSNumber = 95
let str : String = number.stringValue
Singhak
  • 8,508
  • 2
  • 31
  • 34
2

In Swift 3.0

let number:NSNumber = 25
let strValue = String(describing: number as NSNumber)
print("As String => \(strValue)")

We can get the number value in String.

Gautam Sareriya
  • 1,833
  • 19
  • 30