iOS学习笔记---oc语言第七天

类的扩展

NSDate是Cocoa中用于处理日期和时间的基础类,封装了某一给定的时刻,具体的日期 时间和时区

使用+date方法获取当前日期和时间

1 NSDate *date = [NSDate date];//获取当前时间
2     NSLog(@"%@",date);//无论你是哪个时区的时间,打印的总是o时区的时间
3     NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:60*60*8];//从现在开始之后的时间
4     NSLog(@"%@",date2);
5     NSTimeInterval date3 = [date timeIntervalSince1970];//获取时间间隔  从1970年到现在的时间间隔
6     NSLog(@"%f",date3);

NSTimeInterval

即double类型,用以表示以秒为单位的时间间隔

可以使⽤用-initWithTimeIntervalSinceNow:⽅法传入一个NSTimeInterval参数,创建⼀个 NSDate对象

例如:NSDate * tomorrowDate = [[NSDate alloc] initWithTimeIntervalSinceNow:24*60 * 60];

NSDate * yesterdayDate = [[NSDate alloc] initWithTimeIntervalSinceNow:-1 * 24*60 *60];

取两个时间对象的间隔:NSTimeinterval = [tomorrowDate timeIntervalSinceDate:yesterdayDate];

 1     NSDateFormatter *format = [[NSDateFormatter alloc] init];
 2     [format setDateFormat:@"YYYY-MM-dd  HH:mm:ss"];
 3     //YYYY代表4位的年
 4     //MM代表两位的月
 5     //dd代表两位的日
 6     //HH代表24进制小时
 7     //mm代表两位的分钟
 8     //ss代表两位的秒
 9     //NSDateFormatter功能是NSString 与 NSDate 互转
10     //1.NSDate转换为NSString
11     NSString *datestr = [format stringFromDate:date];
12     NSLog(@"%@",datestr);
13     
14     NSDate *newdate = [format dateFromString:@"2014-07-02 11:54:03"];
15     NSLog(@"%@",newdate);

 Category

Category也叫分类或类⺫

主要作用是为 没有源代码 的类添加方法。

通过Category添加的⽅法会成为原类的一部分。从⽽而达到扩展一 个类的功能。

一般扩展系统或者第三方类库

定义Category

新建⽂件

选择Objective-C Category模板

填写类名和分类名

.h⽂件添加⽅法声明

.m添加方法实现

需要使⽤的地方进⾏行调用

Category的声明

NSString+SayHi.h⽂件

@interface NSString (SayHi)

- (void)hi;

@end

Category的实现

NSString+SayHi.m文件

#import “NSString+SayHi.h”

@implementation NSString(SayHi)

- (void)hi{
    

    NSLog(@”这是通过category为NSString添加的hi方法”);
}

@end

Category与子类的区别

 练习:

    使⽤用Category为NSDate类添加⼀个类⽅方法

    (dateWithDateString:),实现将下述字符串转换为NSDate对象。 将@“20140402142850”转换为NSDate。

    NSLog(@"%@",[NSDate dateWithString:@"20140402142850"]);

NSDate+DateWithString.h中代码
#import <Foundation/Foundation.h>

@interface NSDate (DateWithString)
+ (NSDate *)dateWithString:(NSString *)str;
@end
NSDate+DateWithString.m中代码
#import "NSDate+DateWithString.h"

@implementation NSDate (DateWithString)
+ (NSDate *)dateWithString:(NSString *)str
{
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"YYYYMMddHHmmss"];
    NSDate *date = [format dateFromString:str];
    return date;
}
@end
main.m中代码
    NSLog(@"%@",[NSDate dateWithString:@"20140402142850"]);

 Extension

主要作用是为类添加"私有"方法

在.h中没有声明,在.m中有实现

有时候方法比较复杂的时候,使用私有方法结合组成一个复杂方法,,外部实现只用声明复杂方法,不用理会私有方法的实现

 1 #import "Person.h"
 2 @interface Person()//这样就写成Extension,不写也可以,写出来只是为了管理,给程序看的,让其他开发者一眼能看出来那些是"私有"方法
{
NsInteger _age;//定义一个私有变量
}

3 - (void)eat;

4 @end
 5 
 6 @implementation Person
 7 - (instancetype)initWithName:(NSString *)name sex:(NSString *)sex
 8 {
 9     self = [super init];
10     if (self) {
11         _name = name;
12         _sex = sex;
13     }
14     [self eat];
15     return self;
16 }
17 - (void)eat//只有实现,没有声明 .m文件中其他方法可以调用
18 {
19     NSLog(@"这是一个私有方法,只有自己的.m文件中使用eat");
20 }
21 @end

Protocol 协议

协议是一套标准(一堆方法的声明),只有.h文件

接受协议的对象实现协议中定义的方法

#import <Foundation/Foundation.h>

@protocol MyProtocol <NSObject>//< >中定义的的这个协议也遵守了NSObject协议
@required//默认是必须实现的  可以省略,省略代表必须实现
- (void)method1;
- (void)method2;
@optional//可选择是否实现
- (void)method3;
@end

 遵循协议

#import “MarryProtocol”

@interface Boy : NSObject <MarryProtocl> ...

@end

@implementation Boy

//协议中⽅法的实现

@end

一个结婚的协议例子

//协议MarryProtocol.h中文件
#import <Foundation/Foundation.h>

@protocol MarryProtocol <NSObject>

- (void)washcloth;

- (void)cook;

- (void)dohousework;

- (void)makeMoney;

@optional
- (void)hitDouDou;

@end
//Girl.h中代码
#import <Foundation/Foundation.h>
#import "MarryProtocol.h"

@interface Girl : NSObject
{
    NSInteger _age;//年龄
    id <MarryProtocol> _husband;
    
}
- (instancetype)initWithAge:(NSInteger)age;
//设置年龄
- (void)setAge:(NSInteger)age;
//获得年龄
- (NSInteger)age;

- (void)work;

- (void)setHusband:(id<MarryProtocol>)husband;

- (void)hungry;
- (void)dirty;
@end
//Girl.m中代码
#import "Girl.h"

@implementation Girl
- (instancetype)initWithAge:(NSInteger)age
{
    self = [super init];
    if (self) {
        _age = age;
    }
    return self;
}
- (void)hungry
{
    [_husband cook];
}
- (void)setAge:(NSInteger)age
{
    _age = age;
}

- (NSInteger)age
{
    return _age;
}

- (void)work
{
    NSLog(@"接拍电影,广告,电视剧");
}

- (void)setHusband:(id<MarryProtocol>)husband
{
    _husband = husband;
}
- (void)dirty
{
    [_husband washcloth];
}
@end
//Boy.h中代码
#import <Foundation/Foundation.h>
#import "MarryProtocol.h"
@interface Boy : NSObject <MarryProtocol>
- (void)work;
@end
//Boy.m中代码
#import "Boy.h"

@implementation Boy
- (void)work
{
    NSLog(@"我是一个牛逼替身");
}

- (void)washcloth
{
    NSLog(@"洗衣服");
}

- (void)cook
{
    NSLog(@"做饭");
}

- (void)dohousework
{
    NSLog(@"做家务");
}

- (void)makeMoney
{
    NSLog(@"赚钱");
}
- (void)hitDouDou
{
    NSLog(@"打豆豆");
}
@end
//main.m中代码
#import <Foundation/Foundation.h>

#import "Girl.h"
#import "Boy.h"

int main(int argc, const char * argv[])
{


    Girl *liuyifei = [[Girl alloc] initWithAge:27];
    [liuyifei work];
    //数年之后
    Boy *wangwu = [[Boy alloc] init];
    [wangwu work];
    
    //慢慢熟悉
    //...
    //走到一起
    //丈母娘
    //不同意
    
    [liuyifei setHusband:wangwu];
    [liuyifei hungry];
    [liuyifei dirty];
    
    //n年之后,wangwu富有了
    [liuyifei setHusband:nil];


    
    
    
    return 0;
}
原文地址:https://www.cnblogs.com/limicheng/p/3819551.html