OC-Foundation框架

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

Foundation框架下的常用类

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

一.【NSNumber】===============================================

【注】像int、float、char、double等这种都是基础数据类型。

【注】继承自C语言的基础变量类型(int,float,char、double等)不能被添加到数组和字典等oc专有的数据结构中。使用不方便,也不能通过添加类别等oc专有语法进行管理。

【另】可以认为,NSNumber是基础类型数据转成对象类型数据的一个类。

【注】NSNumber 就是一个类,这个类就是为了把基础数据类型转成对象数据类型的一个类。

【注】可以先将基础类型的数据存入到nsnumber对象中,再将nsnumber存入到数组或者字典中。

【注】NSNumber能干的事情都可以用NSString来取代。所以,更常用NSString。

  

二.【NSDate】=====================================

【注】NSDate就是一个日期(时间)类。

【注】因为存在时区差异,获取的时间差8小时

∆1 // 当前时间创建NSDate

                NSDate *myDate = [NSDate date];

                NSLog(@"myDate = %@",myDate);

 ∆       2 //从现在开始的24小时

                NSTimeInterval secondsPerDay = 24*60*60;

                NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];

                NSLog(@"myDate = %@",tomorrow);

        3//根据已有日期创建日期

                 NSTimeInterval secondsPerDay1 = 24*60*60;

                NSDate *now = [NSDate date];

                NSDate *yesterDay = [now addTimeInterval:-secondsPerDay1];

                NSLog(@"yesterDay = %@",yesterDay);

         

   ∆      4//比较日期

                BOOL sameDate = [now isEqualToDate:yesterDay];

                NSLog(@"sameDate = %lu",sameDate);

    ∆            4.1//获取较早的日期

                NSDate *earlierDate = [yesterDay earlierDate:now];

                NSLog(@"earlierDate  = %@",earlierDate);

    ∆            4.2//较晚的日期

                NSDate *laterDate = [yesterDay laterDate:now];

                NSLog(@"laterDate  = %@",laterDate);

三. NSCalendar=====================================

//通过NSCALENDAR类来创建日期

//通过NSCalendar这个类可以自己创建一个指定的日期

        

        NSDateComponents *comp = [[NSDateComponents alloc]init];

        [comp setMonth:06];

        [comp setDay:01];

        [comp setYear:2001];

        [comp setHour:24];

        NSCalendar *myCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

        NSDate *myDate1 = [myCal dateFromComponents:comp];

        

        NSLog(@"myDate1 = %@",myDate1);

//从已有日期获取日期

        NSCalendar *myCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

        unsigned units  = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;

        

        NSDateComponents *comp1 = [myCal components:units fromDate:[NSDate date]];

        NSInteger month = [comp1 month];

        NSInteger year = [comp1 year];

        NSInteger day = [comp1 day];

ΔΔ四【NSDateFormatter】格式化日期类==================================

NSDateFormatter 的一些格式介绍 

//实例化一个NSDateFormatter对象

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

//设定时间格式,这里可以设置成自己需要的格式

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

//用[NSDate date]可以获取系统当前时间

NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];

//输出格式为:2016-03-23 13:22:13

NSLog(@”%@”,currentDateStr);

自定义格式

     [dateFormatter setDateFormat:@"yyyy- MM-dd HH:mm:ss"]; //这里要注意的是formatter的格式,如果是小写的"hh",那么时间将会跟着系统设置变成12小时或者 24小时制。大写的"HH",则强制为24小时制。 

     [dateFormatter setDateFormat:@"yyyy年MM月dd日#EEEE"];EEEE为星期几,EEE为周几 

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

     [dateFormatter setDateFormat:@"yyyy年MMMMd日"];//MMMM 为xx月,一个d可以省去01日前的0

输出格式通setDateStyle和setTimeStyle设置,分别定义的日期和时间的格式可选一下的系统给出的方法

typedef enum {

    NSDateFormatterNoStyle     = kCFDateFormatterNoStyle,

    NSDateFormatterShortStyle  = kCFDateFormatterShortStyle,//“11/23/37” or “3:30pm”

    NSDateFormatterMediumStyle = kCFDateFormatterMediumStyle,//"Nov 23, 1937"

    NSDateFormatterLongStyle   = kCFDateFormatterLongStyle,//"November 23, 1937” or “3:30:32pm"

    NSDateFormatterFullStyle   = kCFDateFormatterFullStyle//“Tuesday, April 12, 1952 AD” or “3:30:42pm PST”

} NSDateFormatterStyle;

五.日期比较========================================

日期之间比较可用以下方法

    - (BOOL)isEqualToDate:(NSDate *)otherDate;

    与otherDate比较,相同返回YES

    - (NSDate *)earlierDate:(NSDate *)anotherDate;

    与anotherDate比较,返回较早的那个日期

    - (NSDate *)laterDate:(NSDate *)anotherDate;

    与anotherDate比较,返回较晚的那个日期

    - (NSComparisonResult)compare:(NSDate *)other;

    该方法用于排序时调用:

      . 当实例保存的日期值与anotherDate相同时返回NSOrderedSame

      . 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending

      . 当实例保存的日期值早于anotherDate时返回NSOrderedAscending

六.NSDate转NSString互相转化======================================

//NSDate 转NSString 并截取月份

NSString* time = [[NSString alloc]initWithFormat:@"%@",myDate1];

        NSLog(@"%@",time);

        NSRange range = {5,2};

        NSLog(@"month:%@",[time substringWithRange:range]);

Δ七.iOS-NSDate 相差 8 小时 ==============================

//方法一

- (void)tDate

{

    NSDate *date = [NSDatedate];

    NSTimeZone *zone = [NSTimeZonesystemTimeZone];

    NSInteger interval = [zone secondsFromGMTForDate: date];

    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];  

    NSLog(@"%@", localeDate);

}

//方法二

+ (NSString *)fixStringForDate:(NSDate *)date 

{

    NSDateFormatter* dateFormatter = [[NSDateFormatteralloc]init];

    [dateFormatter setDateStyle:kCFDateFormatterFullStyle];

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

    NSString *fixString = [dateFormatter stringFromDate:date]; 

    [dateFormatter release];

    return fixString;

}

ΔΔ【NSNull】

【注】表示空的事物有四个

【NULL】【nil】【Nil】【NSNull】

NULL:表示基础类型指针为空

int * p = NULL;

nil:表示对象指针为空

id obj = nil;

Nil:表示Class变量为空

Class class = Nil;

NSNull:用在数组字典等数据结构中占位,作为空元素

//唯一方法

[NSNull null]; 创建表示空的对象

八.归档 NSKeyedArchiver============================================

//创建了一个数组,初始化了一些数据

        NSArray* array = [[NSArray alloc]initWithObjects:@"zhangsan",@"lisi",@"wanger",@"xiaoming", nil];

        

        //先指定要保存的文件名称以及路径

        //NSHomeDirectory()就是当前系统的home路径

        //stringByAppendingPathComponent 添加一个文件,文件名是:file

        //文件类型可以不写,文件名称和后缀随便

        NSString* filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"file.txt"];

        

        NSLog(@"归档文件路径:%@",filePath);

        

        //NSKeyedArchiver 这个类是用来归档的类

        //返回值代表归档是否成功

        //【注】如果对同一个路径下的同一个文件进行归档操作,就会覆盖掉旧的。

        BOOL isSuccess = [NSKeyedArchiver archiveRootObject:array toFile:filePath];

        if (isSuccess == YES) {

            NSLog(@"文件保存成功");

        }

        

        //【注】归档的文件都是经过简单加密的,打不开,也是不允许打开的。

        

        //解归档

        

        NSArray* Arr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

        NSLog(@"取出的数据是:%@",Arr);

NSDate的常用用法==============================================
1. 创建或初始化可用以下方法 ----------------------------------------------------

用于创建NSDate实例的类方法有 + (id)date;
返回当前时间 

+ (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs; 返回以当前时间为基准,然后过了secs秒的时间 

    +

(id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)

secs;

返回以2001/01/01 GMT为基准,然后过了secs秒的时间 

    +

(id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;

返回以1970/01/01 GMT为基准,然后过了secs秒的时间 

    + (id)distantFuture;

返回很多年以后的未来的某⼀一天。 比如你需要⼀一个比现在(Now)晚(大)很长时间的时间值,则可以调用该方法。 

    + (id)distantPast;

  返回很多年以前的某⼀一天。

比如你需要⼀一个比现在(Now)早(小)大很长时间的时间值,则可以调用该方 法。 

用于创建NSDate实例的实例方法有
- (id)addTimeInterval:(NSTimeInterval)secs; 返回以目前的实例中保存的时间为基准,然后过了secs秒的时间 

用于初始化NSDate实例的实例方法有 - (id)init; 初始化为当前时间。类似date方法 

    -

(id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)

secs;

初始化为以2001/01/01 GMT为基准,然后过了secs秒的时间。类似 dateWithTimeIntervalSinceReferenceDate:方法 

    - (id)initWithTimeInterval:(NSTimeInterval)secs

sinceDate:(NSDate *)refDate;

初始化为以refDate为基准,然后过了secs秒的时间 

- (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs; 初始化为以当前时间为基准,然后过了secs秒的时间 

3. 取回时间间隔可用以下方法 ------------------------------------------------------------------

    - (NSTimeInterval)timeIntervalSinceDate:(NSDate

*)refDate;

以refDate为基准时间,返回实例保存的时间与refDate的时间间隔 

    - (NSTimeInterval)timeIntervalSinceNow;

以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时 间间隔 

    - (NSTimeInterval)timeIntervalSince1970;

以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT 的时间间隔 

    - (NSTimeInterval)timeIntervalSinceReferenceDate;

以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT 的时间间隔 

    + (NSTimeInterval)timeIntervalSinceReferenceDate;

以2001/01/01 GMT为基准时间,返回当前时间(Now)与2001/01/01 GMT 的时间间隔 

*/ 

// 时区类
// 获得系统时区 

NSTimeZone *tz = [NSTimeZone systemTimeZone];
// 获得当前时间距离GMT时间相差的秒数!
NSInteger seconds = [tz secondsFromGMTForDate:[NSDate 

date]];
// 以[NSDate date]时间为基准,间隔seconds秒后的时间! NSDate *localDate = [NSDate 

dateWithTimeInterval:seconds sinceDate:[NSDate date]]; // 北 京时间 

           NSLog(@"localDate = %@",localDate);

// 日期和字符串相互转换 

// 日期格式的类!
NSDateFormatter *df = [[NSDateFormatter alloc] init]; // 2013-11-27 11:00:57
// HH 24小时进制 hh 12小时进制
df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *dateStr = [df stringFromDate:[NSDate date]]; NSLog(@"dateStr = %@", dateStr); 

       NSDate *dateTest = [df dateFromString:@"2013-11-27

11:00:57"];

NSLog(@"dateTest = %@", dateTest);//默认是格林威治时间需 要转化为北京时间 

NSTimeZone *tz = [NSTimeZone systemTimeZone]; // 获得dateTest距离GMT时间相差的秒数! NSInteger seconds = [tz 

secondsFromGMTForDate:dateTest];

NSDate *localDate = [NSDate dateWithTimeInterval:seconds sinceDate: dateTest]; // 北京时间 

       NSLog(@"localDate = %@",localDate);

【阅读官方文档】

【注】建议阅读Xcode官方,官方文档非常标准的,没有错误。

//缺点:

1.可读性比较差。(读不懂)(官方文档都比较言简意赅)

2.例子比较少,很少有参照demo。

//优点

1.知识严谨。

2.当遇到某些特殊(疑难杂症)官方文档都可以找到答案(前提是花费时间去阅读查找)

原文地址:https://www.cnblogs.com/GJ-ios/p/5313304.html