zoukankan      html  css  js  c++  java
  • 进击的Objective-C-----------------类目(category),延展(Extension),协议(Protocol),代理(delegate)-委托 时间获取

    1.NSDate(拿到零时区时间)
     NSDate *date1 = [NSDate date];
    2.NSTimZone(拿到本时区)
    NSTimeZone *zone = [[NSTimeZone alloc]init];
    zone = [NSTimeZone localTimeZone];
    3.NSTimeInterval
    NSInteger offset = [zone secondsFromGMT];// 计算时差
    NSDate *date2 = [[NSDate date] initWithTimeIntervalSinceNow:offset];// 本时区的时间
    NSTimeInterval time1 = [[NSDate date] timeIntervalSinceDate:date2];//  计算时间差
    4.NSDateFormatter( ios 开发中的日期时间格式 用来实现字符串和日期之间的转换)
        NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
        [formatter1 setDateFormat:@"yyyy年MM月dd日HH时mm分ss秒"];
        NSString *s = [formatter1 stringFromDate:[NSDate date]];
        NSLog(@"%@",s);//  通过字符串获得日期
        NSDate *dat = [formatter1 dateFromString:s];
        NSLog(@"%@",dat);
    注意:时间格式根据需求去修改([formatter1 setDateFormat:@"HH;mm"];// 微信)     
    5.练习
    1⃣️练习一
        NSDate *date3 = [NSDate date];
        NSTimeZone *zone2 = [[NSTimeZone alloc]init];
        zone2 = [NSTimeZone localTimeZone];
        NSInteger offset2 = [zone2 secondsFromGMT];
        NSDate *nowDate = [date3 initWithTimeIntervalSinceNow:offset2];
        NSDate *fixedDate = [nowDate dateByAddingTimeInterval:360000];
        NSTimeInterval time11 = [date3 timeIntervalSinceDate:fixedDate];
        double time22 = fabs(time11);
        if (time22 < 60)
        {
            NSLog(@"刚刚");
        }
        else if ((time22 > 3600) && (time22 < 3600*24))
        {
            double minutes = time22 / 60;
            NSLog(@"%f分钟前",minutes);
        }
        else if (time22 >3600*24)
        {
        double hours = time22 /3600;
        NSLog(@"%f小时前",hours);
        }
    2⃣️练习二
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒 "];
        NSDate *ss = [dateFormatter dateFromString:@"2014年05月01日 18时23分18秒"];
        NSLog(@"%@",ss);
    重点:
    类目(category),延展(Extension),协议(Protocol),代理(delegate)-委托
    category:类目是给没有源代码的类添加属性的方法,类目添加出来的文件 类名+方法名 创建出来是一对.h和.m文件
    .h文件
    - (void)qxq_sayHi;
    + (void)vip_eat;
    .m文件
    + (void)vip_eat{
        NSLog(@"takaqinojia!!");
    }
    - (void)qxq_sayHi{
        NSLog(@"boomshakalaka!!!");
    main.m
    NSString *s1 = @"权志龙";
        [s1 qxq_sayHi];
        [NSString vip_eat];
    }
    Extension:延展是用来管理私有变量和私有方法的,延展通常直接在该类的.m文件中直接声明即可,格式:@interface + 类名 ,原来的类方法可以直接调用,延展中的私有方法类外无法访问,延展是封装特性的的体现
    .h文件
    @interface Person : NSObject{
        NSString *_Name;
    }
    - (void)eat;
    @end
    .m文件
    @interface Person (){
        NSInteger _age;
    }
    - (void)drink;// 方法只写声明
    @end
    @implementation Person
    - (void)eat{
        NSLog(@"eat too much!!");
    }
    - (void)drink{
        NSLog(@"drink too much!!");
    }
    @end
    main.m
    Person *p1 = [[Person alloc]init];
        [p1 eat];
    Protocol:
    Protocol.h文件
      @protocol MyProtocol <NSObject>
    // 如果只有.h文件 而且里面有Protocol这样的关键字,这是一个协议
    // 协议里面都是方法的声明
    @required// 要求的 方法需要在遵循协议的类的.m文件中实现,否则会有警告 如果不写关键字 默认在协议里的方法都是需要实现的
    - (void)eat;
    - (void)drink;
    @optional // 可以实现的 使用@optional关键字修饰的方法 可以不实现
    - (void)think;
    @end
    .h文件
    #import <Foundation/Foundation.h>
    #import "MyProtocol.h"
    @interface Person : NSObject<MyProtocol>
    // 遵循协议 首先引入 然后再类名后+<协议名>
    @end
    .m文件
    @implementation Person
    - (void)eat{
        NSLog(@"eat too much!!");
    }
    - (void)drink{
        NSLog(@"drink too lot!!");
    }
    - (void)think{
        NSLog(@"think too long!!");
    }
    @end
    main.m
    Protocol 协议
    // 协议只有.h文件里面写方法的声明
    // 遵循协议的对象需要事先协议里的方法
    delegate: 代理就是某个功能,某个类不想自己去实现,找个别的类去实现,这时候需要先定义一个协议,符合协议的类就可以作为代理,实现这个功能 
    步骤:
     // 1 创建一个协议
     // 2 有一个类 里面有一个代理的实例变量 还想让代理实现自己的需求
     // 3 另外一个类协议并实现方法
     // 4 设置代理
     // 5 完成需求
    协议.h文件
    // 协议是来写一堆方法和声明的
    - (void)cook;
    - (void)makeMoney;
    - (void)programming;
    需求类.h
    #import <Foundation/Foundation.h>
    #import "BFProtocol.h"
    @interface Girl : NSObject //<BFProtocol>
    {
        id<BFProtocol>_delegate;
    }
    - (void)setDelegate:(id<BFProtocol>)delegate;
    - (void)hungry;
    - (void)spendMoney;
    - (void)play;
    @end
    需求类.m
    #import "Girl.h"
    @implementation Girl
    - (void)setDelegate:(id<BFProtocol>)delegate{
        _delegate = delegate;
    }
    - (void)hungry{
        [_delegate cook];
    }
    - (void)spendMoney{
        [_delegate makeMoney];
    }
    - (void)play{
        [_delegate programming];
    }
    @end
    代理类.h
    #import <Foundation/Foundation.h>
    #import "BFProtocol.h"
    @interface Boy : NSObject<BFProtocol>
    // 遵循协议
    @end
    代理类.m
    #import "Boy.h"
    @implementation Boy
    - (void)cook{
        NSLog(@"cook eat");
    }
    - (void)makeMoney{
        NSLog(@"随便刷!!");
    }
    - (void)programming{
        NSLog(@"女神!这是我给你写的程序,随便用!没bug!");
    }
    @end
    main.m
    #import <Foundation/Foundation.h>
    #import "Girl.h"
    #import "Boy.h"
    int main(int argc, const char * argv[]) {
        Girl *g1 = [[Girl alloc]init];
        Boy *b1 = [[Boy alloc]init];
        [g1 setDelegate:b1];
        [g1 hungry];
        [g1 play];
        [g1 spendMoney];
  • 相关阅读:
    Linux 系统目录 以及常见命令
    设计模式C++学习笔记之十三(Decorator装饰模式)
    设计模式C++学习笔记之二(Proxy代理模式)
    Linux 文件基本操作
    Linux 文件系统基本结构 以及命令行 管理
    Linux 入门 bash语句 第三课
    JMeter学习参数化User Defined Variables与User Parameters
    JMeter使用中遇到的问题:Jmeter Debug
    JMeter学习(九)FTP测试计划
    JMeter学习(八)JDBC测试计划-连接Oracle
  • 原文地址:https://www.cnblogs.com/sharkHZ/p/4984011.html
Copyright © 2011-2022 走看看