Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-c convert NSString into NSInteger


I've got this little problem. When I have a string "3 568 030" and I use [myString intValue]; it gives me result just 3, the problem is I want to convert the whole number into int/nsinteger. The string always contains just the number if that's any help. I tried using replaceoccurencesofstring (or what is the name) and it somehow didn't work...
Thanks

like image 236
haluzak Avatar asked Oct 10 '11 17:10

haluzak


2 Answers

Do:

NSString *str = @"3 568 030";

int aValue = [[str stringByReplacingOccurrencesOfString:@" " withString:@""] intValue];
NSLog(@"%d", aValue);

output

3568030

like image 93
Jonas Schnelli Avatar answered Oct 10 '22 02:10

Jonas Schnelli


That is because of the spaces on your string you will have to remove the whitespaces first like this:

NSString *trimmedString = [myString stringByReplacingOccurrencesOfString:@" " withString:@""];

NSInteger *value = [trimmedString intValue];
like image 20
Oscar Gomez Avatar answered Oct 10 '22 03:10

Oscar Gomez