且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将地址字符串转换为地址字典

更新时间:2023-09-06 12:11:40

如果您正在使用类型为NSTextCheckingTypeAddress的数据检测器,则返回的匹配项应提供

If you are using a data detector with type NSTextCheckingTypeAddress, the returned matches should provide an addressComponents dictionary.
That dictionary contains all the keys you mentioned.

NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeAddress error:nil];
NSString* addressString = @"Apple, 1 Infinite Loop, Cupertino, CA";
NSArray* matches = [detector matchesInString:addressString options:0 range:NSMakeRange(0, addressString.length)];
for(NSTextCheckingResult* match in matches)
{
    if(match.resultType == NSTextCheckingTypeAddress)
    {
        NSDictionary* addressComponents = [match addressComponents];
        NSLog(@"Address Dictionary:%@", addressComponents);
    }
}

上面的代码段为输入字符串返回以下内容:

The above snippet returns the following for the input string:

Address Dictionary:{
    City = Cupertino;
    State = CA;
    Street = "1 Infinite Loop";
}

功能齐全的地址字符串将产生以下键的值:

A fully featured address string would result in values for the following keys:

NSTextCheckingNameKey
NSTextCheckingJobTitleKey
NSTextCheckingOrganizationKey
NSTextCheckingStreetKey
NSTextCheckingCityKey
NSTextCheckingStateKey
NSTextCheckingZIPKey
NSTextCheckingCountryKey
NSTextCheckingPhoneKey
NSTextCheckingAirlineKey
NSTextCheckingFlightKey