OC完全解读:Foundation框架下的NSValue,NSNumber ,NSNull,NSData,NSCalendarDate

来源:互联网 发布:淘宝哪些精仿mcm的店铺 编辑:程序博客网 时间:2024/04/29 16:41
OC完全解读:Foundation框架下的NSValue,NSNumber ,NSNull,NSData,NSCalendarDate


在OC的类集(NSArray,NSDictionary,NSSet)中,所存储的都是对象,而面向对象的开发思想中:一切皆是对象。那么,如何将基本数据类型和结构体转化为对象呢?
1.NSValue:将结构体转化为对象

- (instancetype)initWithBytes:(constvoid *)value objCType:(constchar *)type; 

+ (NSValue *)valueWithBytes:(constvoid *)value objCType:(constchar *)type;

+ (NSValue *)value:(constvoid *)value withObjCType:(constchar *)type;

//value参数是想要包装的数据地址(如一个NSPoint的地址,可以用&来取地址),type参数是用来描述这个数据类型的字符串,用@encode指令生成

以上三个方法是将自定义的结构体转化为对象,例子如下:

        typedef struct{            int a ;            float b ;        }Data ;                Data dataElem  ;                dataElem.a = 10 ;                dataElem.b = 10.005 ;                NSValue *value = [NSValue valueWithBytes:&dataElem objCType:@encode(Data)] ;                Data getDateElem ;                [value getValue:&getDateElem] ;                NSLog(@"%d,%0.3f",getDateElem.a,getDateElem.b);

运行结果如下:

2015-07-29 21:57:28.689 demo3[597:17386] 10,10.005

- (void)getValue:(void *)value;

//获取所包装的数据,保存到value这个地址

+ (NSValue *)valueWithPointer:(constvoid *)pointer;

- (void *)pointerValue;

//以上两个方法是是利用结构体的地址将结构体转换为对象

+ (NSValue *)valueWithPoint:(NSPoint)point;

+ (NSValue *)valueWithSize:(NSSize)size;

+ (NSValue *)valueWithRect:(NSRect)rect;

+ (NSValue *)valueWithEdgeInsets:(NSEdgeInsets)insets

//将常见结构体转化为对象,以上4个方法尤为重要。

@property (readonly)NSPoint pointValue;

@property (readonly)NSSize sizeValue;

@property (readonly)NSRect rectValue;

@property (readonly)NSEdgeInsets edgeInsetsValue

//常见结构 体转化为对象后,使用以上方法转化为接结构体

代码如下:

        CGPoint point =CGPointMake(100,100);

        NSValue *value = [NSValue valueWithPoint:point];  //结构体转化对象

        CGPoint point2 = [value pointValue];

        NSLog(@"%f -> %f",point2.x,point2.y);//对象转化结构体


2.NSNumber:将基本数据类型包装成对象

NSNumber是NSValue的子类,但是NSNumber只包装数字类型,NSValue可以包装任何值,NSNumber将基本数据类型包装成对象后,这样就可以间接的把基本数据类型放入NSArray、NSDictionary中。

+ (NSNumber *)numberWithChar:(char)value;

+ (NSNumber *)numberWithUnsignedChar:(unsignedchar)value;

+ (NSNumber *)numberWithShort:(short)value;

+ (NSNumber *)numberWithUnsignedShort:(unsignedshort)value;

+ (NSNumber *)numberWithInt:(int)value;

+ (NSNumber *)numberWithUnsignedInt:(unsignedint)value;

+ (NSNumber *)numberWithLong:(long)value;

+ (NSNumber *)numberWithUnsignedLong:(unsignedlong)value;

+ (NSNumber *)numberWithLongLong:(longlong)value;

+ (NSNumber *)numberWithUnsignedLongLong:(unsignedlonglong)value;

+ (NSNumber *)numberWithFloat:(float)value;

+ (NSNumber *)numberWithDouble:(double)value;

+ (NSNumber *)numberWithBool:(BOOL)value;

//将对应的数据类型装包,也就是转化为对象;用法与【将常见结构体转化为对象】类似:

@property (readonly)char charValue;

@property (readonly)unsignedchar unsignedCharValue;

@property (readonly)short shortValue;

@property (readonly)unsignedshort unsignedShortValue;

@property (readonly)int intValue;

@property (readonly)unsignedint unsignedIntValue;

@property (readonly)long longValue;

@property (readonly)unsignedlong unsignedLongValue;

@property (readonly)longlong longLongValue;

@property (readonly)unsignedlonglong unsignedLongLongValue;

@property (readonly)float floatValue;

@property (readonly)double doubleValue;

@property (readonly)BOOL boolValue;

//将对应的数据类型解包,也就是对象转化为基本数据类型;用法与【将NSValue体转化为对象常见结构】类似:

代码如下:

        int num =12;

        NSNumber *numValue = [NSNumber numberWithInt:num];  //int数据类型转化为对象(装包)

        num = [numValue intValue];//对象转化int数据类型(解包)

3、NSNull:
集合中是不允许放nil值,因为nil在集合中有特殊意义,但是有时候确实需要存储一个表示“什么都没有”的价值,那么就可以使用NSNull,它是NSObject的一个子类

NSNull *n = [NSNull null];//是单例的,只能返回一个单例对象
NSNull *n1 = [NSNull null];//
NSNull *n2 = [NSNull null];//
这三个指针变量指向同一个变量

4.NSData

使用文件时,需要频繁地将数据读入一个临时存储区,它通常成为缓冲区

NSData类提供了一种简单的方式,它用来设置缓冲区、将文件的内容读入缓冲区,或将缓冲区的内容写到一个文件。

对于32位应用程序,NSDATA缓存区最多可以存储2GB的数据。

我们既可定义不变缓冲区(NSData类),也可定义可变的缓冲区(NSMutableData类)。

下面代码展示了如何将文件的内容读入内存缓冲区,然后再将缓冲区的内容写入到另一个文件中。

NSData*fileData;

NSFileManager *fileManager=[[NSFileManager alloc]init];

fileData=[fileManager contentsAtPath:path];  

[fileManager createFileAtPath:path2 contents:fileData attributes:nil];   //采用默认的属性值

类型转换 NSData -> NSString

NSString *strData = [[NSString alloc]initWithData:fileData encoding:NSASCIIStringEncoding];

类型转换 NSString -> NSData

NSData *fileData2 = [strData dataUsingEncoding:NSUTF8StringEncoding];

5.NSCalendarDate

NSCalendarDate对象包含了日期和时间、时区以及一个带有格式的字符串,它从NSDate继承而来。

NSCalendarDate对象是immutable的,一旦被创建,无法修改其中的时间和日期,当然可以修改那个带格式的字符串和时区。

以下是常用方法:

+(id)calendarDate;  //创建当前日期和时间以及默认格式的NSCalendarDate对象,时区为机器设置好的时区。

+(id)dateWithYear:(int)year

    month:(unsigned)month

      day:(unsigned)day 

     hour:(unsigned)hour

   minute:(unsigned)minute

   second:(unsigned)second 

 timeZone:(NSTimeZone  *)aTimeZone 

-(int)dayOfCommonEra;  //得到从公元1年算起,有多少天

-(int)dayOfMonth;          //返回是月的第几天(1-31

-(int)dayOfWeek;          //返回是周的第几天0-6

-(int)dayOfYear;          //返回是年的第几天(1-366

-(int)hourOfDay;          //返回是日的第几个小时(0-23

-(void)setCalendarFormate:(NSString *)format 

--------创建NSCalendarDate对象--------

NSCalendarDate *now;

now = [NSCalendarDate calendarDate];

NSTimeZone *pacific = [NSTimeZone timeZoneWithName:@"PST"];

NSCalendarDate *hotTime = [NSCalendarDate dateWithYear:2011 month:2 day:3 hour:14 minute:0 second:0 timeZone:pacific];




0 0
原创粉丝点击