ObjectC----NSDate和Extension以及代理协议的用法

来源:互联网 发布:药品说明书查询软件 编辑:程序博客网 时间:2024/06/10 17:12

  

    NSDate * nowDate = [NSDate date];

    NSLog(@"%@",nowDate);

    //距离现在3600*24秒的时间日期,单位秒

    NSDate * tomorrow = [NSDate dateWithTimeIntervalSinceNow:3600*24];

    NSLog(@"%@",tomorrow);

    NSDate * date = [NSDate dateWithTimeIntervalSince1970:365*24*3600];

    NSLog(@"%@",date);

    // 计算两个日期之间的时间差,单位是秒

    NSTimeInterval interval = [nowDate timeIntervalSinceDate:tomorrow];

    NSLog(@"%f",interval);

    NSTimeInterval interval2 = [nowDate timeIntervalSince1970];

    NSLog(@"%f",interval2);

    


    // 创建当前时间和一个固定时间

    NSDate *now = [NSDate date];

    NSDate *past = [NSDate dateWithTimeIntervalSinceNow:-50];

    // 计算两个时间的时间差

    NSTimeInterval interval3 =[now timeIntervalSinceDate:past];

    if (interval3 <=60) {

        NSLog(@"ganggang");

    }

    else if(interval3>60 && interval3<=3600){

        NSLog(@"xxfenzhong");

    }

    else{

        NSLog(@"xx");

    }

   // ============================================

    // NSDateFormatter日期和字符串之间的转化

    

    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];

    // 必须设置日期格式

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//也可@"yyyyMMdd"

    // 将日期转化成字符串,这样打印的时候才是自己位置时区的时间

    NSDate * nowDate2 = [NSDate date];

    NSString *str = [formatter stringFromDate:nowDate2];

    NSLog(@"%@",str);

    //将字符串转化成日期

    NSDate * newDate = [formatter dateFromString:@"2000-1-1 10:12:23"];

    NSLog(@"%@",newDate);

    //练习

    NSDateFormatter *formatter5 = [[NSDateFormatter alloc] init];

    // [formatter5 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    [formatter5 setDateFormat:@"yyyyMMdd HH:mm:ss"];

    NSDate * newDate4 = [formatter5 dateFromString:@"20140501 10:23:18"];

     NSString * newDate5 = [formatter5 stringFromDate:newDate4];

    

    NSLog(@"%@",newDate5);

    // ===============================================================


   //(分类)类目

    /*

     1.类目主要用于在不知道源码的情况下扩展类的功能;

     2.类目只能为类添加方法,不能添加实例变量;

     */

    // ===============================================================

    // Extension:管理私有方法



    //===============================================================



   /*代理模式步骤:

     1,定义协议 BaoMuProtocol

     2,遵循协议的类  (Nurse)

     3.定义需要代理的类  (Mother)

     4.建立关系   (Mother中定义一个代理类型的成员变量)     

     */

  BaoMuProtocol:

  @required

  - (void)takeCareOfBaby;


  @optional

  - (void)cook;

  - (void)doHouseWork;


  @end

===================

Nurse.h

#import "BaoMuProtocol.h"

@interface Nurse : NSObject<BaoMuProtocol>


@end

Nurse.m

- (void)takeCareOfBaby

{

    NSLog(@"Nurse--takeCareOfBaby");

}



- (void)cook

{

    NSLog(@"Nurse--cook");

}

- (void)doHouseWork

{

    NSLog(@"Nurse--doHouseWork");

}

=================================
Mother.h

#import "BaoMuProtocol.h"


@interface Mother : NSObject

{

    id<BaoMuProtocol> _delegate;

}


- (void)setDelegate:(id<BaoMuProtocol>)delegate;

- (id<BaoMuProtocol>)delegate;


- (void)work;



@end

Mother.m

 (void)setDelegate:(id<BaoMuProtocol>)delegate

{

    _delegate = delegate;

}

- (id<BaoMuProtocol>)delegate

{

    return _delegate;

}


- (void)work

{

    NSLog(@"Mother--Work");

    [_delegate takeCareOfBaby];

}

=============================
main中调用

    Mother * mother = [[Mother alloc] init];

    Nurse * nurse = [[Nurse alloc] init];

    [mother setDelegate:nurse];

    [mother work];

=============================

0 0
原创粉丝点击