標籤:
ios開發基礎資料型別 (Elementary Data Type)和結構體的封裝與解鎖 -- 妖妖
//知識:
//因為基礎資料型別 (Elementary Data Type)和結構體不是繼承自NSObject,所以它們不可以直接存放到數組和字典中。
//數組和字典中只能儲存物件類型,其他基本類型和結構體是沒有辦法放到數組和字典中的,當然你也是無法給它們發送訊息的(也就是說有些NSObject的方法是無法調用的),這個時候通常會用到裝箱(boxing)和拆箱(unboxing)。但是在ObjC中裝箱的過程必須手動實現,ObjC不支援自動裝箱。
//在ObjC中我們一般將基礎資料型別 (Elementary Data Type)裝箱成NSNumber類型(當然它也是NSObject的子類,但是NSNumber不能對結構體裝箱),調用其對應的方法進行轉換:
@interface c (NSNumberCreation)
//基礎資料型別 (Elementary Data Type)的封裝
+ (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);
//基礎資料型別 (Elementary Data Type)的解鎖
@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的子類,它封裝了一些基礎資料型別 (Elementary Data Type)的常用裝箱、拆箱方法,而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開發)基礎資料型別 (Elementary Data Type)和結構體的封裝與解鎖