你的位置:首页 > 软件开发 > 操作系统 > Foundation框架下的常用类:NSNumber、NSDate、NSCalendar、NSDateFormatter、NSNull、NSKeyedArchiver

Foundation框架下的常用类:NSNumber、NSDate、NSCalendar、NSDateFormatter、NSNull、NSKeyedArchiver

发布时间:2016-03-24 09:00:14
==========================Foundation框架下的常用类========================== 一.【NSNumber】【注】像int、float、char、double等这种都是基础数据类型。【注】继承自C语言的基础变量类型(in ...

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

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);

 

自定义格式

   // 这里要注意的是formatter的格式,如果是小写的"hh",那么时间将会跟着系统设置变成12小时或者 24小时制。大写的"HH",则强制为24小时制。 

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

     [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);

【阅读官方文档】

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

// 缺点:

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

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

 

// 优点

1.知识严谨。

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

【解决编程中问题的方法】

1.可以先百度

2.请去Google。(Google出来答案99%英文文档,配合有道词典进行查阅)

3.请去请教同事。

4.请看官方文档。

5.请配合有道词典查看。

 

 


原标题:Foundation框架下的常用类:NSNumber、NSDate、NSCalendar、NSDateFormatter、NSNull、NSKeyedArchiver

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录