[objective-c] How to convert NSNumber to NSString

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

This question is related to objective-c nsstring nsarray nsnumber

The answer is


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.


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

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

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


In Swift you can do like this

let number : NSNumber = 95
let str : String = number.stringValue

You can do it with:

NSNumber *myNumber = @15;
NSString *myNumberInString = [myNumber stringValue];

Examples related to objective-c

Adding a UISegmentedControl to UITableView Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Warp \ bend effect on a UIView? Use NSInteger as array index Detect if the device is iPhone X Linker Command failed with exit code 1 (use -v to see invocation), Xcode 8, Swift 3 ITSAppUsesNonExemptEncryption export compliance while internal testing? How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem? Change status bar text color to light in iOS 9 with Objective-C

Examples related to nsstring

NSRange to Range<String.Index> Creating NSData from NSString in Swift How do I check if a string contains another string in Swift? Converting NSString to NSDictionary / JSON convert NSDictionary to NSString How do I deserialize a JSON string into an NSDictionary? (For iOS 5+) How do I URL encode a string Objective-C and Swift URL encoding Convert an NSURL to an NSString Remove all whitespaces from NSString

Examples related to nsarray

Initialize Array of Objects using NSArray How to convert NSNumber to NSString NSDictionary to NSArray? Convert NSArray to NSString in Objective-C How do I convert NSMutableArray to NSArray? NSArray + remove item from array How do I iterate over an NSArray? Using NSPredicate to filter an NSArray based on NSDictionary keys How can I reverse a NSArray in Objective-C? filtering NSArray into a new NSArray in Objective-C

Examples related to nsnumber

How to convert NSNumber to NSString Convert NSNumber to int in Objective-C How to convert an NSString into an NSNumber