// 1.声明协议
// @protocol协议关键词
***Girl.h***
@protocol Marry <NSObject>
// 协议的内容
// 方法声明
// @required是必须实现的方法,默认必须实现
// @optional是可选择执行的方法
@required
- (void)makeMoney;
@optional
- (void)cook;
// 2.设置代理人属性
@property(nonatomic, assign)id<Marry>delegate;
// 结婚后,协议才正式生效
- (void)getMarry;
// 3.让代理人去执行协议里的方法
***Girl.h***
- (void)getMarry {
[self.delegate makeMoney];
}
// 4.引完头文件后,boy需要签订协议
Boy.h
@interface Boy : NSObject<Marry>
// 5.设置代理人
***main.m***
Boy *boy = [[Boy alloc] init];
Girl *girl = [[Girl alloc] init];
girl.delegate = boy;
// 调用结婚的方法,来执行内部协议方法
[girl getMarry];
// 6.实现方法makeMoney
***Boy.m***
- (void)makeMoney {
NSLog(@"买买买");
}
// 类目
// 系统写好的类目:按功能对系统的类方法进行区分
// 类目从@interface开始,后面是当前类的名字,类名后是分类的功能,到@end结束
// 我们创建的类目,一般是为了把一些系统的类进行功能扩充
***NSString+StringMethod.h***
- (void)sayHi;
// 输入两个日期,判断两日期是否在同一个月
- (BOOL)isEqualTwoDate:(NSString *)otherDate;
// 拼接两个日期
- (NSString *)format:(NSString *)otherDate;
// 给定两个时间,判断时间是否在两个时间区内
- (BOOL)inTimeZone:(NSString *)begin end:(NSString *)end;
// 传一个字符串,返回时间对象NSDate类型
- (NSDate *)stringChangeToDate;
+ (NSDate *)stringChangeToDate:(NSString *)date;
***NSString+StringMethod.m***
- (void)sayHi {
NSLog(@"yo, what's up mother fucker!");
}
- (void)sayHello {
NSLog(@"sun of beach!");
}
- (BOOL)isEqualTwoDate:(NSString *)otherDate {
// 这个方法需要两个参数,第一个参数就是方法的调用者,谁调用了方法,方法内部的self就是谁
if ([[self substringWithRange:NSMakeRange(5, 2)] isEqualToString:[otherDate substringWithRange:NSMakeRange(5, 2)]]) {
[self sayHi];
return YES;
} else {
[self sayHello];
return NO;
}
}
- (NSString *)format:(NSString *)otherDate {
NSString *strNew = [self substringWithRange:NSMakeRange(0, 16)];
NSString *strNew2 = [otherDate substringWithRange:NSMakeRange(11, 5)];
[self sunOfBeach];
return [NSString stringWithFormat:@"%@ - %@",strNew, strNew2];
}
// 不写声明的方法,只能在类的.m文件中使用(私有方法)
- (void)sunOfBeach {
NSLog(@"yoooo");
}
// 判断时间是否在时间区域内
- (BOOL)inTimeZone:(NSString *)begin end:(NSString *)end {
NSString *str = [self substringWithRange:NSMakeRange(8, 2)];
NSInteger time = [str integerValue];
NSInteger beginTime = [[begin substringWithRange:NSMakeRange(8, 2)] integerValue];
NSInteger endTime = [[end substringWithRange:NSMakeRange(8, 2)] integerValue];
return time >= beginTime && time <= endTime;
}
- (NSDate *)stringChangeToDate {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
return [formatter dateFromString:self];
}
+ (NSDate *)stringChangeToDate:(NSString *)date {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
return [formatter dateFromString:date];
}