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