码迷,mamicode.com
首页 > 移动开发 > 详细

(ios开发)基本数据类型和结构体的封装与解封

时间:2015-08-31 09:57:50      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:

ios开发基本数据类型和结构体的封装与解封 -- 妖妖

//知识:

//因为基本数据类型和结构体不是继承自NSObject,所以它们不可以直接存放到数组和字典中。

//数组和字典中只能存储对象类型,其他基本类型和结构体是没有办法放到数组和字典中的,当然你也是无法给它们发送消息的(也就是说有些NSObject的方法是无法调用的),这个时候通常会用到装箱(boxing)和拆箱(unboxing)。但是在ObjC中装箱的过程必须手动实现,ObjC不支持自动装箱。

 

//在ObjC中我们一般将基本数据类型装箱成NSNumber类型(当然它也是NSObject的子类,但是NSNumber不能对结构体装箱),调用其对应的方法进行转换:

@interface c (NSNumberCreation)

//基本数据类型的封装

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

+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;

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

+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;

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

+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;

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

+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;

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

+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;

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

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

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

+ (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);

+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);

 

//基本数据类型的解封

@property (readonly) char charValue;

@property (readonly) unsigned char unsignedCharValue;

@property (readonly) short shortValue;

@property (readonly) unsigned short unsignedShortValue;

@property (readonly) int intValue;

@property (readonly) unsigned int unsignedIntValue;

@property (readonly) long longValue;

@property (readonly) unsigned long unsignedLongValue;

@property (readonly) long long longLongValue;

@property (readonly) unsigned long long unsignedLongLongValue;

@property (readonly) float floatValue;

@property (readonly) double doubleValue;

@property (readonly) BOOL boolValue;

@property (readonly) NSInteger integerValue NS_AVAILABLE(10_5, 2_0);

@property (readonly) NSUInteger unsignedIntegerValue NS_AVAILABLE(10_5, 2_0);

@property (readonly, copy) NSString *stringValue;

 

例子:

//封装

    NSNumber *number=[NSNumber numberWithInt:2];

    NSArray *array=[NSArray arrayWithObject:number];

//解封

    [number intValue];

 

 

//结构体呢的封装和解封需要引入另外一个类型NSValue,其实上面的NSNumber就是NSValue的子类,它包装了一些基本数据类型的常用装箱、拆箱方法,而NSValue可以对任何数据类型进行装箱、拆箱操作。

 

//对于常用的结构体Foundation已经为我们提供好了具体的装箱方法如下:

@interface NSValue (NSValueUIGeometryExtensions)

//结构体的封装

+ (NSValue *)valueWithCGPoint:(CGPoint)point;

+ (NSValue *)valueWithCGVector:(CGVector)vector;

+ (NSValue *)valueWithCGSize:(CGSize)size;

+ (NSValue *)valueWithCGRect:(CGRect)rect;

+ (NSValue *)valueWithCGAffineTransform:(CGAffineTransform)transform;

+ (NSValue *)valueWithUIEdgeInsets:(UIEdgeInsets)insets;

+ (NSValue *)valueWithUIOffset:(UIOffset)insets NS_AVAILABLE_IOS(5_0);

 

//结构体的解封

- (CGPoint)CGPointValue;

- (CGVector)CGVectorValue;

- (CGSize)CGSizeValue;

- (CGRect)CGRectValue;

- (CGAffineTransform)CGAffineTransformValue;

- (UIEdgeInsets)UIEdgeInsetsValue;

- (UIOffset)UIOffsetValue NS_AVAILABLE_IOS(5_0);

 

//封装

    CGPoint point=CGPointMake(10, 20);

    NSValue *value=[NSValue valueWithCGPoint:point];//对于系统自带类型一般都有直接的方法进行包装

    NSArray *array=[NSArray arrayWithObject:value];//放倒数组中

//解封

     CGPoint point1=[value CGPointValue];

 

(ios开发)基本数据类型和结构体的封装与解封

标签:

原文地址:http://www.cnblogs.com/yaoyao0110/p/4772192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!