NSString To NSNumber

Andrew picture Andrew · Apr 1, 2013 · Viewed 41.5k times · Source

So this problem has been perplexing me for way too long. I have a UIAlertView with a textField in it, and I need the value of the textField as an NSNumber. But everything I try gives me random strings of numbers. Any help would be very much appreciated.

int i = [[alertView textFieldAtIndex:0].text intValue];
NSNumber *number = [NSNumber numberWithInt:[[alertView textFieldAtIndex:0].text integerValue]];    


int number = [[dict objectForKey:@"integer"] intValue];

NSLog(@"text = %@", [alertView textFieldAtIndex:0].text);


NSString *alertText = [alertView textFieldAtIndex:0].text;

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterNoStyle];
NSNumber * myNumber = [f numberFromString:[alertView textFieldAtIndex:0].text];


NSNumber *number = @([alertText intValue]);

NSString *string = @"54";
NSNumber *number = @([string intValue]);

NSLog(@"here we are: %i", number);

Answer

Balu picture Balu · Apr 1, 2013

Once see this one,

NSString *string = @"123";
NSNumber  *aNum = [NSNumber numberWithInteger: [string integerValue]];
NSLog(@"%@",aNum);//NSString to NSNumber
NSInteger number=[string intValue];
NSLog(@"%i",number);//NSString to NSInteger