Data Detectors

The NSDataDetector class is a subclass of NSRegularExpression. Data detectors allow you to search for well-defined data types, including dates, addresses, URL links, phone numbers, and transit information, using Apple’s fully tested algorithms instead of trying to create your own regular expressions. Even better, they’re localized!

Take the same approach shown previously for enumerating regular expressions. This code snippet searches for links (URLs) and phone numbers:

NSError *error = NULL; NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:         NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber     error:&error]; // Enumerate over a string [detector enumerateMatchesInString:text ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.