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
}
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.
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.
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.