NSDataDetector

There’s immense value in transforming natural language into structured data that’s compatible with our calendars, address books, maps, and reminders. Manual data entry, however, amounts to drudgery, and is the last thing you want to force on users.

On other platforms, you might delegate this task to a web service or hack something together that works well enough. Fortunately for us Cocoa developers, Foundation us covered with NSDataDetector.

You can use NSDataDetector to extract dates, links, phone numbers, addresses, and transit information from natural language text.

nshipster.com/nsdatadetector/

NSDataDetector finds dates, links, phone numbers, addresses, and transit details in natural language text. It is a subclass of NSRegularExpression.

To use it, create an instance with the types you want. Then call enumerateMatches to get NSTextCheckingResult objects. Check each result's type and pull out details like date, URL, or address parts.

For example, it can spot a flight time, work out its length, and note time zones. But it may miss some facts, like hotel names or airline codes.

The API feels old and lacks some features found in UITextView or WKWebView, such as shipment tracking. Still, it helps turn messy text into useful data without web services.


Category:

Year: