本文介绍了在Swift 3.0中使用NSDataDetector从字符串中提取地址元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 NSDataDetector 来处理字符串中的地址.我看过 NSHipster在 NSDataDetector 上的文章以及 Apple的NSDataDetector文档.我有以下方法可以将地址从字符串中拉出:

I'm attempting to use NSDataDetector to addresses from a string. I've taken a look at NSHipster's article on NSDataDetector as well as Apple's NSDataDetector documentation. I've got the following method to the point where it'll pull addresses out of a string:

func getAddress(from dataString: String) -> [String] {
    let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.address.rawValue)
    let matches = detector.matches(in: dataString, options: [], range: NSRange(location: 0, length: dataString.utf16.count))

    var addressArray = [String]()

    // put matches into array of Strings
    for match in matches {
        let address = (dataString as NSString).substring(with: match.range)
        addressArray.append(address)
    }

    return addressArray
}

我想提取地址的元素,而不是整个地址.在数据检测器匹配类型中的 NSHipster的 NSDataDetector 帖子中.部分,我看到了诸如 NSTextCheckingCityKey NSTextCheckingStateKey NSTextCheckingZIPKey .我无法在 NSDataDetector 的初始化中使用这些键.

I'd like to pull out elements of addresses, not the entire address. In NSHipster's NSDataDetector post in the Data Detector Match Types section, I see address components such as NSTextCheckingCityKey, NSTextCheckingStateKey, and NSTextCheckingZIPKey. I'm unable to use those keys in the NSDataDetector's initialization.

我在GitHub上挖了一下,看看是否可以找到一个婴儿床的例子,但是我唯一能找到的东西是Swift主仓库中的Objective-C代码或声明性东西.

I dug around on GitHub to see if I could find an example to crib from, but the only stuff I'm able to find is Objective-C code or declarative stuff in the master Swift repo.

我有99%的把握可以提取地址的各个组成部分,但我太笨了,无法弄清楚.感谢您的阅读.我欢迎您提出建议.

I'm 99% sure I can pull out the individual components of an address, but I'm too dumb to figure it out. Thank you for reading. I welcome suggestions.

推荐答案

我以前没有使用过此类,但看起来它返回了 NSTextCheckingResult 类型的对象.如果得到类型为 NSTextCheckingTypeAddress 的结果,则可以要求结果为它的 addressComponents ,它将是包含地址不同部分的字典.

I haven't used this class before, but it looks like it returns objects of type NSTextCheckingResult. If you get a result of type NSTextCheckingTypeAddress then you can ask the result for it's addressComponents, which will be a dictionary containing the different parts of the address.

这是我刚刚打过的一些有效的游乐场代码:

Here is some working playground code I just banged out:

import UIKit

var string = "Now is the time for all good programmers to babble incoherently.\n" +
"Now is the time for all good programmers to babble incoherently.\n" +
"Now is the time for all good programmers to babble incoherently.\n" +
"123 Elm Street\n" +
"Daton, OH 45404\n" +
"Now is the time for all good programmers to babble incoherently.\n" +
"2152 E Street NE\n" +
"Washington, DC 20001"

let results = getAddress(from: string)

print("matched \(results.count) addresses")
for result in results {
  let city = result[NSTextCheckingCityKey] ?? ""
  print("address dict = \(result).")
  print("    City = \"\(city)\"")
}

func getAddress(from dataString: String) -> [[String: String]] {
  let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.address.rawValue)
  let matches = detector.matches(in: dataString, options: [], range: NSRange(location: 0, length: dataString.utf16.count))

  var resultsArray =  [[String: String]]()
  // put matches into array of Strings
  for match in matches {
    if match.resultType == .address,
      let components = match.addressComponents {
      resultsArray.append(components)
    } else {
      print("no components found")
    }
  }
  return resultsArray
}

此代码显示:

address dict = ["Street":"123 Elm Street","ZIP":"45404","City":"Daton","State":"OH"].城市=道顿"

address dict = ["Street": "123 Elm Street", "ZIP": "45404", "City": "Daton", "State": "OH"]. City = "Daton"

address dict = ["Street":"2152 E Street NE","ZIP":"20001","City":华盛顿","State":"DC"].城市=华盛顿"

address dict = ["Street": "2152 E Street NE", "ZIP": "20001", "City": "Washington", "State": "DC"]. City = "Washington"

这篇关于在Swift 3.0中使用NSDataDetector从字符串中提取地址元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 22:52