zoukankan      html  css  js  c++  java
  • OC 07 类的扩展

    1.NSDate的使用

    NSDate是Cocoa中⽤于处理⽇期和时间的基础类,封装了某⼀给定的时刻(含日期,时间,时区) 

    注意NSLog(@“%@”,nowDate);⽆论你是哪个时区的时间,打印时总是打印对应的0时区时间。 

    2.NSTimelnterval

    NSTimelnterval(即double类型)

    常用方法:

    可以使用-initWithTimeIntervalSinceNow:方法传⼊一个NSTimeInterval参数,创建一个 NSDate对象 

     
     1 // NSDate: 处理时间的类
     2     //1.创建NSDate对象
     3     //    + (instancetype)date;
     4     NSDate *nowDate =[NSDate date];
     5     NSLog(@"%@",nowDate);
     6     
     7     //NSTimeInterval 间隔时间秒数 double类型
     8     //创建明天现在的时间
     9 //    - (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
    10     NSDate *tomorrowDate = [[ NSDate alloc]initWithTimeIntervalSinceNow:24*3600];
    11     NSLog(@"%@",tomorrowDate);
    12     //创建昨天现在的时间
    13     NSDate *yesterdayDate = [[ NSDate alloc]initWithTimeIntervalSinceNow:-24*3600];
    14     NSLog(@"%@",yesterdayDate);
    15     //取两个时间对象的间隔
    16 //    - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;
    17     NSTimeInterval time1 = [ tomorrowDate timeIntervalSinceDate:yesterdayDate]/3600;
    18     NSLog(@"%.0f",time1);
    19     //练习
    20 //    + (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
    21     NSDate *datetime2 = [ NSDate dateWithTimeIntervalSinceNow:23 * 2342 ];
    22 //    - (NSTimeInterval)timeIntervalSinceNow;
    23     NSTimeInterval time2 = [ datetime2 timeIntervalSinceNow];
    24     if (time2 < 60) {
    25         NSLog(@"刚刚");
    26     }else if (time2 > 60 && time2 < 3600){
    27         NSLog(@"%.f前",time2/60);
    28     }else if (time2 > 3600){
    29         NSLog(@"%.0f小时前",time2/3600);
    30     }

    3.NSDateFormatter

    NSDateFormatter是iOS中的⽇日期格式类,功能是实现NSString和NSDate的互

    转。 

    常⻅见的时间格式化字符串有以下⼀一些:y 年、 M 年中的⽉月份 、d ⽉月份中的天 数、 H ⼀一天中的⼩小时数(0-23)、 h am/pm 中的⼩小时数(1-12)、m ⼩小时中的分钟 数 、s 分钟中的秒数 等 

    指定⽇日期格式: NSDateFormatter * formatter = [[NSDateFormatter alloc] init];

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

     //创建日期时间格式化类NSDateFormatter
        //设置时间格式化样式
        NSDateFormatter *dateFormatter = [[ NSDateFormatter alloc]init];
    //    - (void)setDateFormat:(NSString *)string;
        [dateFormatter setDateFormat:@"yyyy年MM月dd日  HH时mm分ss秒"];
        
    //    - (NSString *)stringFromDate:(NSDate *)date;
        NSString *timestr =[ dateFormatter stringFromDate:[NSDate date]];
        NSLog(@"%@",timestr);
    //    将字符串@“2014年05⽉01⽇ 10点23分18秒”转换为NSDate对象。
    //    @"2014年11月21日  11时42分49秒" 格式必须相同
         NSString *timestring = @"2014年11月21日  11时42分49秒";
    //    - (NSDate *)dateFromString:(NSString *)string;
        NSDate *datetimer = [ dateFormatter dateFromString:timestring];
        NSLog(@"datefromString:%@",datetimer);

    4.Category

    Category也叫分类或类⽬

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

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

    如何定义Category

    .h里添加方法声明

    #import <Foundation/Foundation.h>
    // @inteface 后面紧跟的类名表示的是你要给哪个类扩充方法
    // (Hi) : 它中hi表示的时当前类目的名字
    @interface NSString (Hi)
    
    + (void)sayHi;
    @end

    .m添加方法实现

    @implementation NSString (Hi)
    
    + (void)sayHi
    {
        NSLog(@"hai");
    }
    

     

    @end

    5.Extension

    Extension的主要作用是管理类的“私有”方法。面向对象编程也叫⾯面向接⼝编程。

    在设计⼀个类的时候,有些方法需要对外公开(我们称为接口), 有些方法仅内部使用(⽐如:是某个方法的一个小步骤)。

    Extension的功能是帮我们去管理这些内部使用的方法(“私有”方法)。 

    Extension的语法格式和Category很相似 相当于把Category的.h⽂文件挪到了原始类的.m⽂件中 Extension针对的是自⼰己的类,必须有源代码的类 

    如何定义Extension?

    xxx.m⽂文件
    @interface xxx (ExtensionName) //你的方法列表
    @end
    @implementation xxx //方法的实现
    @end 

    #import "Person.h"
    
    
    //延展 Extension
    @interface Person ()
    
    - (void)introduceSelf;
    
    @end
    
    
    @implementation Person
    
    - (void)introduceSelf
    {
        NSLog(@"hello");
    }
    @end

    Category与Extension的区别

    6.Protocol(协议),是iOS开发中常⽤用的技术。

    协议是一套标准(一堆⽅法的声明),只有.h文件。就像一张任务清单(或便利贴),上面写了一堆需要处理的事。清单交给谁,谁就要去完成清单上规定的任务。

     接受协议的对象实现协议中定义的⽅法。即:清单交给谁,谁就要去完成清单上规定的任务。

    如何定义Protocol

    @protocol开始,以@end结束,例如:

    @protocol MarryProtocol <NSObject>

    - (void)makeMoney;
    @optional
    - (void)washcloth;

    - (void)cook;

    @end 

    协议中的⽅法默认是必须实现的,即@required。 关键字 @optional 修饰的方法是可选的,可实现也可不实现。 

    遵守协议分两步

    1、在类的.h文件 父类名后写上<协议名>。

    2、在.m文件中实现协议中的方法。相当于给这个类添加了若干个方法。这个类的实例就可以调用这些方法.

    如何遵守协议?

    #import “MarryProtocol”

    @interface Boy : NSObject <MarryProtocl> ...
    @end
    @implementation Boy
    //协议中⽅法的实现
    @end 

    delegate

    Protocol的核⼼使用场景是实现delegate设计模式。

    delegate(代理)。通俗的讲就是代理商,主要的任务是帮你完成一些任务。

    比如:保姆就可以认为是delegate,主要的任务是帮你带孩子、 做饭、洗衣服等。 

    使⽤场景:凡是某些任务⾃⼰不去实现,想让别人去实现的时候, 就可以指定⼀个代理,让代理帮你去做。你只需要通知代理去做某事。

     


  • 相关阅读:
    LeetCode Best Time to Buy and Sell Stock
    LeetCode Scramble String
    LeetCode Search in Rotated Sorted Array II
    LeetCode Gas Station
    LeetCode Insertion Sort List
    LeetCode Maximal Rectangle
    Oracle procedure
    浏览器下载代码
    Shell check IP
    KVM- 存储池配置
  • 原文地址:https://www.cnblogs.com/panny/p/4114391.html
Copyright © 2011-2022 走看看