Extension---Use + understanding of the Iosdcategory category

Source: Internet
Author: User

Category in iOS is a kind of extension way, it is like Zhao Benshan and song Dandan of the "egg-laying Rooster" sketch, the rooster is crowing, he will not lay eggs, but give him a new function is to lay eggs! This is the category in the class, the role of! (Don't throw up the groove!!) Thank you!! )
Note that: Gategory can only be extended (instance method and class method) cannot add instance variable (personal feeling is necessary!) Can instance variable add why do not inherit it! )
The code is as follows:
Nsstring+newmethod.h

#import <Foundation/Foundation.h>@interface NSString (CommonlyMethod)/** *  判断是否是邮箱 * *  @return 结果 */-(BOOL)isValidateEmail;/** *  判断是否是手机号 * *  @return 结果 */-(BOOL) isValidateMobile;/** * 获取当前时间戳 * 毫秒数形式 * */+(NSString*)TimeJab;@end

Nsstring+newmethod.m

#import "Nsstring+newmethod.h"  @implementation nsstring (commonlymethod)-(BOOL) isvalidateemail{NSString*emailregex = @"[A-z0-9a-z._%+-][email protected][a-za-z0-9.-]+\\. [A-za-z] {2,4} "; Nspredicate *emailtest = [Nspredicate predicatewithformat:@"Self MATCHES%@", Emailregex];return[Emailtest Evaluatewithobject: Self];} -(BOOL) isvalidatemobile{//Mobile number starts with 13, 15, 18, eight \d numeric characters    NSString*phoneregex = @"^ ((13[0-9)) | ( 15[^4,\\D]) | (18[0,0-9])) \\d{8}$ "; Nspredicate *phonetest = [Nspredicate predicatewithformat:@"Self MATCHES%@", Phoneregex];return[Phonetest Evaluatewithobject: Self];}//Get current timestamp+(NSString*) Timejab {NstimeintervalTime = [[NSDateDate] timeIntervalSince1970];Long Long intNewTime = time * +;//Timesecond = NewTime;    NSString* Timestr = [NSStringstringwithformat:@"%lld", NewTime];returnTimestr;}@end

Ok!!! Get a handle on the mailbox and the phone number and the method of getting the timestamp extension complete!!
Test using:

#import "ViewController.h"  @interface viewcontroller ()@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];NSString*STR = @"13520489903";//---If it's email    if([str isvalidateemail]) {NSLog(@"He's an email."); }if([str isvalidatemobile]) {NSLog(@"He's a phone number."); }//--getting the current timestamp    NSLog(@"The time point is---->%@",[NSStringTimejab]);additional setup after loading the view, typically from a nib.}- (void) Didreceivememorywarning {[SuperDidreceivememorywarning];//Dispose of any resources, can be recreated.}

Iosdcategory Category Extensions---use + understanding

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.