Nsdatadetector is a subclass of nsregularexpression, primarily used to detect semi-structured data: dates, addresses, phone numbers, regular expressions, and so on.
typedef ns_options (uint64_t, Nstextcheckingtype) {//a single type nstextcheckingtypeorthography = 1ULL & lt;< 0,//language identification nstextcheckingtypespelling = 1ULL << 1, Spell checking Nstextcheckingtypegrammar = 1ULL << 2,//grammar checking Nstextch Eckingtypedate = 1ULL << 3,//Date/time detection nstextcheckingtypeaddress = 1ULL << 4,//address detection Nstextcheckingtypelink = 1ULL << 5, Link detection nstextcheckingtypequote = 1ULL << 6,//Smart quotes Nste Xtcheckingtypedash = 1ULL << 7,//Smart dashes nstextcheckingtypereplacement = 1ULL << 8,//fixed replacements, such as copyright symbol for (c) nstextcheckingtypecorrection = 1ULL << 9,//AutoCorrection nstextcheckingtyperegularexpression ns_enum_available (10_7, 4_0) = 1ULL << 10, Regular expression matches Nstextcheckingtypephonenumber ns_enum_available (10_7, 4_0) = 1ULL << ; One,//Phone number detection nstextcheckingtypetransitinformation ns_enum_available (10_7, 4_0) = 1ULL <&L T //transit (e.g. flight) info detection};How to use
Nserror *error = nil; Initialize nsdatadetector nsdatadetector *detector = [Nsdatadetector datadetectorwithtypes According to the type of detection: Nstextcheckingtypephonenumber | Nstextcheckingtypelink error:&error]; String to be detected nsstring *TESTSTR = @ "There is a URL: wwww.JohnnyLiu.com has a phone: 15310547654 There is also an address: Tai Tun Road"; There are several ways to detect matching data//1. Detects and then operates on each detected data//-(void) enumeratematchesinstring: (NSString *) string options: (nsmatchingopt ions) Options Range: (nsrange) range Usingblock: (void (^) (Nstextcheckingresult *result, nsmatchingflags flags, BOOL *stop )) Block; 2. Detect the obtained array//-(Nsarray *) matchesinstring: (NSString *) string options: (nsmatchingoptions) options range: (Nsrange) Range 3. Total number of detections//-(Nsuinteger) numberofmatchesinstring: (NSString *) string options: (nsmatchingoptions) options Range: ( Nsrange) range; 4. First detected data//-(Nstextcheckingresult *) firstmatchinstring: (NSString *) string options: (nsmatchingoptions) options Range: (NSRange) range; 5. Range of the first detected data//-(Nsrange) rangeoffirstmatchinstring: (NSString *) string options: (nsmatchingoptions) options Range: (nsrange) range; [Detector enumeratematchesinstring:teststr Options:kniloptions Range:nsmakerange (0, Teststr.length) usingblock:^ (Nstextcheckingresult *result, NSMATCHINGFL AGS flags, BOOL *stop) {//nstextcheckingresult has many properties that match the type of detection, such as Url,phonenumber,date,addres Scomponents and so on.//nstextcheckingresult also has a range and nstextcheckingtype two properties for easy operation NSLog (@ "Result.range =%@", Nsstringfromrange (Result.range)); if (result. URL) {NSLog (@ "url =%@", result.) URL); } if (Result.phonenumber) {NSLog (@ "phone =%@", RESULT.P Honenumber); } }]; Result-------------------------------------------//result.range = {6,//url} = http://wwww.JohnnyLiu.com//resu Lt.range = {+, +}//phone = Tel: 15310547654
Use of Nsdatadetector and Nstextcheckingresult