How to turn an NSString into an NSNumber

We can use an NSNumberFormatter for this. It’s a bit like converting an NSString into an NSDate.

Here’s how:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
NSNumber *myNumber = [formatter numberFromString:self.textField.text];

Assuming that we’d like to collect input from a text field (set to accept number pad input only) myNumber will now be an NSNumber instead of text.





2 thoughts on “How to turn an NSString into an NSNumber

Leave a Reply