在我的应用程序中,我试图根据用户区域设置格式化电话号码。我面临的问题如下:
Viewcontroller.m 中的代码:
NSString *contact = [_phone convertContact:myNumber withLocale:typeN];
NSLog(@"Phone number %@ and number = %@",contact,myNumber);
//Console output 2012-07-23 15:40:16.994 InternProj[7585:f803] value of en and 23443235434
PhoneNumber.m 中的代码:
- (NSString *) convertContact:(NSNumber *)aNumber withLocale:(NSString *)locale{
NSString *localeString = locale ;
NSLog(@"value of %@ and %@",localeString , aNumber);
NSString *tempStr = [[NSString alloc] initWithString:@""];
NSRange range;
range.length = 3;
range.location = 3;
//Returns the phone number 2032225200 as 1(203)222-5200
if([localeString compare:@"en"] == NSOrderedSame) {
NSLog(@"Inside en");
NSString *areaCode = [[aNumber stringValue] substringToIndex:3];
NSString *phone1 = [[aNumber stringValue] substringWithRange:range];
NSString *phone2 = [[aNumber stringValue] substringFromIndex:6];
tempStr = [NSString stringWithFormat:@"1(%@)%@-%@", areaCode, phone1, phone2];
NSLog(@"Value tempStr = %@",tempStr);
}
else {
tempStr = [[aNumber stringValue] substringToIndex:3];
}
return tempStr;
}
我从 convertContact 方法得到一个 null 作为返回值。我尝试了所有的排列,但我没有得到我出错的地方。此外,如果有人有基于 Iphone 区域设置的电话号码格式信息,请分享。