8、category、extension、protocol
来源:互联网 发布:origin画图软件下载 编辑:程序博客网 时间:2024/05/16 10:41
category、extension、protocol
NSObject+print.h
#import <Foundation/Foundation.h>@interface NSObject (print)- (void)print;@end
NSObject+print.m
#import "NSObject+print.h"@implementation NSObject (print)- (void)print{ NSLog(@"%@", self);}@end
NSDate+Transfer.h
#import <Foundation/Foundation.h>@interface NSDate (Transfer)+ (NSDate *)dateWithDateString:(NSString *)str;@end
NSDate+Transfer.m
#import "NSDate+Transfer.h"@implementation NSDate (Transfer)+ (NSDate *)dateWithDateString:(NSString *)str;{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyyMMddHHmmss"]; return [[formatter dateFromString:str] dateByAddingTimeInterval:8 * 60 * 60];}@end
NSString+Other.h
#import <Foundation/Foundation.h>@interface NSString (Other)+ (void)luoBen;@end
NSString+Other.m
#import "NSString+Other.h"@implementation NSString (Other)+ (void)luoBen{ NSLog(@"xx在广阔的草原上,发疯似的狂奔");}@end
Student.h
#import <Foundation/Foundation.h>#import "StudyingProtocol.h"// 在当前类的父类名后,添加 <...> ,内部写协议名称,就代表遵守了该协议,相当于拥有了该协议中所有方法的声明@interface Student : NSObject <StudyingProtocol>- (void)sayHi;- (void)sayHello;@end
Student.m
#import "Student.h"// 私有方法声明放在Student.m的延展中,在Student.m中去实现私有方法,然后只能在内部使用@interface Student ()- (void)daoZhangWenHao;@end@implementation Student- (void)daoZhangWenHao{ NSLog(@"我是道长,我向大家问好。。。");} // 只能在内部使用- (void)sayHi{ NSLog(@"Hi"); [self daoZhangWenHao]; // 只能在内部使用}- (void)sayHello{ NSLog(@"Hello"); [self daoZhangWenHao];}// 实现协议中的方法- (void)goodStudying{ NSLog(@"好好学习,天天向上");}- (void)doHomework{ NSLog(@"认真做作业");}- (void)xiaoFeng{ NSLog(@"我们需要校风。。。");}@end
StudyingProtocol.h
#import <Foundation/Foundation.h>@protocol StudyingProtocol <NSObject>@required // 标注方法是必须实现的- (void)goodStudying;- (void)doHomework;- (void)xiaoFeng;@optional // 标注方法是可选实现的- (void)needWater;- (void)neediPhone;@end
main.m
#import <Foundation/Foundation.h>#import "NSString+Other.h"#import "NSDate+Transfer.h"#import "NSObject+print.h"#import "Student.h"typedef CGFloat (^maxAnsBlock) (CGFloat, CGFloat);int main(int argc, const char * argv[]) { @autoreleasepool { // 求两个浮点数的最大值,用block CGFloat (^maxBlock) (CGFloat, CGFloat) = ^(CGFloat f1, CGFloat f2) { return f1 > f2 ? f1 : f2; }; CGFloat max = maxBlock(23.8, 12.5); NSLog(@"%.2f", max); // 使用block重定义的别名 maxAnsBlock max0 = ^(CGFloat a, CGFloat b) { return a > b ? a : b; }; CGFloat ans = max0(34.6, 23.0); NSLog(@"%.2f", ans); // 1.NSDate // eg1、使用初始化方法,创建一个当前时间对象 NSDate *nowDate = [[NSDate alloc] init]; NSLog(@"nowDate = %@", nowDate); // eg2、在当前时间基础上,添加一天,得到明天这个时候的时间 NSDate *tomorrowThisDate = [nowDate dateByAddingTimeInterval:24 * 60 * 60]; NSLog(@"tomorrowThisDate = %@", tomorrowThisDate); // eg3、在当前时间基础上,减去一天,得到昨天这个时候的时间 NSDate *yesterdayThisDate = [NSDate dateWithTimeInterval:-(24 * 60 * 60) sinceDate:nowDate]; NSLog(@"yesterdayThisDate = %@", yesterdayThisDate); // eg4、用便利构造器,创建一个距离现在1500秒之后的时间 NSDate *afterNowDate = [NSDate dateWithTimeIntervalSinceNow:1500]; NSLog(@"afterNowDate = %@", afterNowDate); // eg5、获取明天和昨天之间相隔的秒数 NSTimeInterval secondInterval = [tomorrowThisDate timeIntervalSinceDate:yesterdayThisDate]; NSLog(@"secondInterval = %.2f", secondInterval); // 2.NSDateFormatter // 创建时间转换的对象 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // 设置时间格式串格式 [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss eee"]; // eee代表“周一”、eeee代表“星期一” // 将NSDate转换为NSString NSString *nowDateStr = [dateFormatter stringFromDate:[NSDate date]]; NSLog(@"时间格式:%@", nowDateStr); // 将NSString转换为NSDate [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss eee"]; NSDate *nowDate0 = [dateFormatter dateFromString:nowDateStr]; // 转换后没有格式 NSLog(@"时间格式:%@", nowDate0); // ----- eg.2 ----- NSString *strDate = @"2015年05月25日 10点23分20秒"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"]; NSDate *date0 = [formatter dateFromString:strDate]; NSLog(@"时间格式:%@", date0); // 3.Category,不能添加实例变量,文件命名:原始类名+类目名 // 如果通过Category添加的方法和系统的方法重复:(尽可能不要重复) // + 方法,后添加的方法优先级高 // - 方法,系统自带的方法优先级高 [NSString luoBen]; NSString *str = @"20140402142850"; NSLog(@"%@", [NSDate dateWithDateString:str]); NSArray *array = [NSArray arrayWithObjects:@"key", @"mon", nil]; [array print]; // 4.Extension } return 0;}
1 0
- 8、category、extension、protocol
- Category、Extension、Protocol
- Category Extension protocol 总结
- Category、Protocol、Extension
- 类目 Category Extension 协议 Protocol
- Category , Extension , Protocol几个概念
- ios之Category,Extension,Protocol,delegate
- category extension protocol(类目,延展,协议)
- OC 类目category,延展extension,协议protocol
- xcode6中如何创建category,extension,protocol
- iOS创建Category Protocol Extension 文件
- Object基础语法Category、Extension、Protocol
- OC第七天 --- NSDate Category Extension Protocol
- 类目category 扩展extension 协议protocol
- self,super,protocol,category,extension 的用法
- OC基础之Category,Extension,Protocol
- Objective-C——Category、Extension、Protocol
- OC_类目(Category)、延展(extension)、协议(Protocol)
- IOS开发之分类扩展
- Kmemleak检测工具介绍
- 好孩子公司电子采购平台系统---选用上海百酷公司电子采购平台系统
- 【莫比乌斯反演】关于ΣΦ(d|n)=n和Σμ(d|n)=0
- [PHP]运用PHPMailer发送带压缩附件的邮件
- 8、category、extension、protocol
- .Net 通过子对象访问父对象
- Overlapped模型深入分析(原理篇)
- fedora 安裝配置DHCP server
- WPF TabControl 竖版简单例子
- Supplier Tax Payer ID
- 推荐!国外程序员整理的 C++ 资源大全
- 地铁站外公开叫卖“学生公交卡”可顺利充值
- java接口实例化问题