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

Macos 如何将地址字符串转换为地址字典,macos,geocoding,addressbook,Macos,Geocoding,Addressbook,我使用OSX数据检测器从文本中提取地址。这给了我地址字符串,如“1无限循环,Cupertino,CA”。 为了将这些地址放入OSX地址簿中,我必须将它们转换为带有“Street”、“City”、“Country”等键的字典。 我知道有一个函数的作用正好相反:ABCreateStringWithAddressDictionary(…),但我需要另一种方法。 我知道唯一可以做到这一点的方法(当然,完全是多余的)是将地址字符串发送到CLGeocoder,后者返回地址的坐标,然后再次发送坐标进行反向地理

我使用OSX数据检测器从文本中提取地址。这给了我地址字符串,如“1无限循环,Cupertino,CA”。
为了将这些地址放入OSX地址簿中,我必须将它们转换为带有“Street”、“City”、“Country”等键的字典。
我知道有一个函数的作用正好相反:
ABCreateStringWithAddressDictionary(…)
,但我需要另一种方法。
我知道唯一可以做到这一点的方法(当然,完全是多余的)是将地址字符串发送到
CLGeocoder
,后者返回地址的坐标,然后再次发送坐标进行反向地理编码,后者返回包含地址字典的位置标记。
必须有更简单的方法来实现这一点。
编辑
我现在已经学会了,
1) 这个问题非常复杂,而且最好使用地理编码服务器,例如,请参见

2) 仅与地理编码服务器联系一次就足够了:当一个地址字符串发送到那里时,一个地址目录将被发送回。

如果您使用的是类型为
NSTextCheckingTypeAddress
的数据检测器,返回的匹配项应提供一个字典。
那本词典包含了你提到的所有钥匙

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);
    }
}
上述代码段为输入字符串返回以下内容:

Address Dictionary:{
    City = Cupertino;
    State = CA;
    Street = "1 Infinite Loop";
}
功能齐全的地址字符串将产生以下键的值:

NSTextCheckingNameKey
NSTextCheckingJobTitleKey
NSTextCheckingOrganizationKey
NSTextCheckingStreetKey
NSTextCheckingCityKey
NSTextCheckingStateKey
NSTextCheckingZIPKey
NSTextCheckingCountryKey
NSTextCheckingPhoneKey
NSTextCheckingAirlineKey
NSTextCheckingFlightKey