Ios 从一个写的数字中得到一个整数

Ios 从一个写的数字中得到一个整数,ios,cocoa-touch,string-parsing,nsnumberformatter,Ios,Cocoa Touch,String Parsing,Nsnumberformatter,我需要从写出的数字中获取整数值,例如,从字符串@“三”中获取int中的“3”。我该怎么做?您可以这样做: NSString * threeString = @"three"; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; formatter.numberStyle = NSNumberFormatterSpellOutStyle; formatter.locale = [NSLocale currentLocal

我需要从写出的数字中获取整数值,例如,从字符串
@“三”
中获取
int
中的“3”。我该怎么做?

您可以这样做:

NSString * threeString = @"three";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterSpellOutStyle;
formatter.locale = [NSLocale currentLocale];
NSNumber * stringNumb = [formatter numberFromString:threeString];
int threeInt = [stringNumb intValue];
NSLog(@"ThreeInt: %i", threeInt);
(Google“parse english number”,你会有一些想法。)见下: