Foundation框架下的常用类(NSNumber, NSValue, NSDate,NSDateFormatter)

1.NSNumber

   将基础数类型数据转成对象数据(比如int  float double BOOL  long等等)

 

 

 //通过NSNumber将基础数类型数据转成对象数据。

 NSNumber * intNumber = [[NSNumber alloc] initWithInt:50];

 NSNumber * floatNumber = [NSNumber numberWithFloat:45.3];

 

 //xcode4.6以后,可以采用如下写法

 NSNumber * doubleNumber = @3.14;

 NSNumber * boolNumber2 = @YES;

 

     

 //从对象中取出基础类型数据

 double d = doubleNumber.doubleValue;

 float f = floatNumber.floatValue;

 

 

 

2. NSValue

      NSValue是NSNumber的父类,可以存储任何类型的数据,包括复合数据类型(数组,指针,结构体等).

 

     int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

     //第一个参数传第一个字节的地址,无所谓类型。

     //第二个参数决定数据的大小。    

     //@encode将类型转成NSValue可以识别的字符串。

     NSValue * value = [NSValue valueWithBytes:a objCType:@encode(int[10])];

 

     //取出数据

     int b[10];

    [value getValue:b];

 

 

 

 

    typedef struct

    {

        int id;

        float height;

        unsigned char flag;

    }Test;  

 

 

    Test test1;

    test1.id = 1;

    test1.height = 23.5;

    test1.flag = 'A';

    NSValue * test = [NSValue valueWithBytes:&test1 objCType:@encode(Test)];

        

    //声明test2,为了得到test1的值

    Test test2;

    //把test的值赋值到test2中

    [test getValue:&test2];

 

 

 

3. NSNull

        如要确实需要存储一个表示“什么都没有”的值,可以使用NSNull类。

        NSNull值为空的对象,可以加入到数组或者词典中 [NSNull null]; 创建表示空的对象

 

     nil -> Null-pointer to objective- c object  ———对象指针为空

    Nil -> Null-pointer to objective- c class  ———类指针为空

    NULL-> null pointer to primitive type or absence of data.  ———基本类型指针为空

 

        空指针不能用来加入到数组或字典,所以可以采用[NSNull null]的方式加入空对象。

 

   NSMutableDictionary *dict = [NSMutableDictionary dictionary];

        [dict setObject:[NSNull null] forKey:@"someKey"];

        //nil 被用来作为集合结束的标志,不能填入到array和dictionary中

        //[dict setObject:nil forKey:@"wrongKey”];———错误的

 

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

课堂练习:

<1> 创建一个数组,穿插存入空对象和字符串

<2>   写一个结构体,存成NSValue,再取出来

 

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

 

4. NSDate

 

        //创建当前时间,以格林尼治时间为准

        NSDate * date = [NSDate date];

        NSLog(@"%@", date);

        

        //某一时间过了多少秒,创建一个新的时间点

        NSDate * date2 = [NSDate dateWithTimeInterval:3600 sinceDate:date];

        NSLog(@"%@", date2);

        

        //从当前时间过了多少秒,生成新的时间点

        NSDate * date3 = [NSDate dateWithTimeIntervalSinceNow:-3600];

        NSLog(@"%@", date3);

        

        //从1970/01/01 0时 GMT为准,过后多少秒,生成新的时间

        NSDate * date4 = [NSDate dateWithTimeIntervalSince1970:3600];

        NSLog(@"%@", date4);

        

        

        //未来时间,用于暂停定时器,将定时器启动时间设为遥远的未来 Never

        NSDate * futureDate = [NSDate distantFuture];

        //过去时间,用于重启定时器,将定时器启动时间设为遥远的过去 ever

        NSDate * pastDate = [NSDate distantPast];

        

        NSLog(@"%@",futureDate);

        NSLog(@"%@",pastDate);

 

        

        //创建时间戳

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

        

        //使用时间戳的目的,是为了自定义时间的打印格式

        dateFormatter.dateFormat = @"yyyy年MM月dd日EEE HH:mm:ss.S";

        

        dateFormatter.dateFormat = @"yyyy-MM-dd ahh时mm分ss秒";

        //HH是24小时制,hh是12小时制  a表示上下午

        //EEE表示周几  EEEE表示星期几

 

        NSLog(@"%@", [dateFormatter stringFromDate:date]);

 

        NSDate *da = [dateFormatter dateFromString:[dateFormatter stringFromDate:date]];

        NSLog(@"%@",da);

 

 

NSDate


//得到当前的日期
 NSDate *date = [NSDate date];
 NSLog(@"date:%@",date);
 
 //得到(24 * 60 * 60)即24小时之前的日期,dateWithTimeIntervalSinceNow:
 NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24 * 60 * 60)];
 NSLog(@"yesterday:%@",yesterday);


NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];
 NSDate *date = [NSDate date];
 [formatter setTimeStyle:NSDateFormatterMediumStyle];
 NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
 NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
 NSInteger unitFlags = NSYearCalendarUnit | 
                       NSMonthCalendarUnit |
                       NSDayCalendarUnit | 
                       NSWeekdayCalendarUnit | 
                       NSHourCalendarUnit |
                       NSMinuteCalendarUnit |
                       NSSecondCalendarUnit;
 //int week=0;
 comps = [calendar components:unitFlags fromDate:date];
 int week = [comps weekday]; 
 int year=[comps year]; 
 int month = [comps month];
 int day = [comps day];
 //[formatter setDateStyle:NSDateFormatterMediumStyle];
 //This sets the label with the updated time.
 int hour = [comps hour];
 int min = [comps minute];
 int sec = [comps second];
 NSLog(@"week%d",week);
 NSLog(@"year%d",year);
 NSLog(@"month%d",month);
 NSLog(@"day%d",day);
 NSLog(@"hour%d",hour);
 NSLog(@"min%d",min);
 NSLog(@"sec%d",sec);

 

//得到毫秒
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
 [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
 //[dateFormatter setDateFormat:@"hh:mm:ss"]
 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
 NSLog(@"Date%@", [dateFormatter stringFromDate:[NSDate date]]);
 [dateFormatter release];

 
  /*----日期时间的比较----*/
// 当前时间
NSDate *currentDate = [NSDate date];
// 比当前时间晚一个小时的时间
NSDate *laterDate = [[NSDate alloc] initWithTimeInterval:60*60 sinceDate:[NSDate date]];
// 比当前时间早一个小时的时间
NSDate *earlierDate = [[NSDate alloc] initWithTimeInterval:-60*60 sinceDate:[NSDate date]];
// 比较哪个时间迟
if ([currentDate laterDate:laterDate]) {
   // 打印结果:current-2013-08-16 09:25:54 +0000比later-2013-08-16 10:25:54 +0000晚
  NSLog(@"current-%@比later-%@晚",currentDate,laterDate);
}
// 比较哪个时间早
if ([currentDate earlierDate:earlierDate]) {
  // 打印结果:current-2013-08-16 09:25:54 +0000 比 earlier-2013-08-16 08:25:54 +0000
  NSLog(@"current-%@ 比 earlier-%@ 早",currentDate,earlierDate);
}
 
if ([currentDate compare:earlierDate]==NSOrderedDescending) {
  // 打印结果
NSLog(@"current 晚");
}
 
if ([currentDate compare:currentDate]==NSOrderedSame) {
  // 打印结果
  NSLog(@"时间相等");
}
 
if ([currentDate compare:laterDate]==NSOrderedAscending) {
// 打印结果
  NSLog(@"current 早");
}

 

转载于:https://www.cnblogs.com/W-Kr/p/5215363.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值