Creating Data Detector Instances

+ dataDetectorWithTypes:error:

Creates and returns a new data detector instance.

Example

NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeAddress | NSTextCheckingTypePhoneNumber | NSTextCheckingTypeLink error:nil];
NSString *myStr = @"(001) 1234-5678 No 1 Central Street. http//eezytutorials.com";
[detector enumerateMatchesInString:myStr options:kNilOptions range:NSMakeRange(0, [myStr length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    NSLog(@"Values: %@", result);
}];
		

Output

2014-04-09 06:21:14.538 iOS-Tutorial[3703:a0b] Values: {0, 15}{(001) 1234-5678}
2014-04-09 06:21:14.651 iOS-Tutorial[3703:a0b] Values: {19, 16}
2014-04-09 06:21:14.652 iOS-Tutorial[3703:a0b] Values: {43, 17}{http://eezytutorials.com}
		

- initWithTypes:error:

Initializes and returns a data detector instance.

Example

NSDataDetector *detector = [[NSDataDetector alloc] initWithTypes:NSTextCheckingTypeAddress | NSTextCheckingTypePhoneNumber | NSTextCheckingTypeLink error:nil];
NSString *myStr = @"(001) 1234-5678 No 1 Central Street. http//eezytutorials.com";
[detector enumerateMatchesInString:myStr options:kNilOptions range:NSMakeRange(0, [myStr length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    NSLog(@"Values: %@", result);
}];
		

Output

2014-04-09 06:22:23.487 iOS-Tutorial[3724:a0b] Values: {0, 15}{(001) 1234-5678}
2014-04-09 06:22:23.488 iOS-Tutorial[3724:a0b] Values: {19, 16}
2014-04-09 06:22:23.489 iOS-Tutorial[3724:a0b] Values: {43, 17}{http://eezytutorials.com}
		

Getting the Checking Types

checkingTypes

Returns the checking types for this data detector.

Example

NSDataDetector *detector = [[NSDataDetector alloc] initWithTypes:NSTextCheckingAllTypes error:nil];
NSLog(@"%d",[detector checkingTypes] == NSTextCheckingAllTypes);		

Output

2014-04-09 06:28:21.996 iOS-Tutorial[3776:a0b] 1
		

Advertisements