代码之家  ›  专栏  ›  技术社区  ›  user1904273

将NSTextCheckInResult addressComponents字典转换为Objective-C中带有DataDetector的字符串

  •  0
  • user1904273  · 技术社区  · 6 年前

    苹果 NSDataDetector NSTextCheckingResults . 对于地址,它捕获字典中的信息,其中包含许多表示地址元素的键,如地址、城市、州和zipcode。 Here are the various keys.

    我的问题是,我对使用字典的能力很弱,无法理解将字典转换回常规字符串的语法。我需要一个字符串,这样我就可以将它输入到地图的自然语言查询中。

    有人能建议将字典转换成字符串的语法吗。

     NSDictionary* addrDict= nil;
        NSString *addr = nil;
        NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeAddress error:&error];
    
        NSArray *matches = [detector matchesInString:string
                                             options:0
                                               range:NSMakeRange(0, [string length])];
    
        NSLocale* currentLoc = [NSLocale currentLocale];
    
        for (NSTextCheckingResult *match in matches) {
            if ([match resultType] == NSTextCheckingTypeAddress) {
    
                addrDict = [match addressComponents];
    //How do I convert this dictionary back into a string that says something like 
             Starbucks 123 Main Street Mountain View CA 94103  
            }
    
        }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Larme    6 年前

    NSTextCheckingResult 有财产 range 您可以使用:

    NSRange addressMatchRange = [match range];
    NSString *matchString = [string substringWithRange:addressMatchRange];
    

    addrDict[NSTextCheckingZIPKey] 我会给你 94103 , addrDict[NSTextCheckingStateKey] CA 等等,你必须重建它,但顺序取决于你。