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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);

 

以上是关于Foundation框架下的常用类(NSNumber, NSValue, NSDate,NSDateFormatter)的主要内容,如果未能解决你的问题,请参考以下文章

iOS 基础04--Foundation框架下基本集合类

iOS 基础04--Foundation框架下基本集合类

34-oc Foundation简介

Foundation框架—字符串

OC学习13——Foundation框架中的集合

OC 知识:Foundation 框架及相关类详尽总结