Use of Nsdatadetector and Nstextcheckingresult

Source: Internet
Author: User

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 <&lt ; One,//Phone number detection nstextcheckingtypetransitinformation ns_enum_available (10_7, 4_0) = 1ULL &LT;&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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.