読みやすい電話番号として書式設定された電話番号を持っています。つまり、(123) 123-4567
ただし、その電話番号をコードで使用する場合は、その文字列を数値変数 ( などint
)に解析する必要があります。
ただし、[string intValue];
機能しません-intValue
以前のプロジェクトで約100万回使用しましたが、すべて問題ありませんでしたが、ここでは、渡すすべての文字列が同じランダムなint
バックアウトを取得します:
- (int)processPhoneNumber:(NSString *)phoneNumber {
NSMutableString *strippedString = [NSMutableString stringWithCapacity:10];
for (int i=0; i<[phoneNumber length]; i++) {
if (isdigit([phoneNumber characterAtIndex:i])) {
[strippedString appendFormat:@"%c",[phoneNumber characterAtIndex:i]];
}
}
NSLog(@"clean string-- %@", strippedString);
int phoneNumberInt = [strippedString intValue];
NSLog(@"**** %d\n &i", phoneNumberInt, phoneNumberInt);
return phoneNumberInt;
}
次に、このメソッドを呼び出すと:
NSLog(@"%i", [self processPhoneNumber:self.phoneNumberTextField.text]);
私は得る: 2147483647
. このメソッドに与えるすべての入力は次を返します。2147483647