(ios開發)基礎資料型別 (Elementary Data Type)和結構體的封裝與解鎖

來源:互聯網
上載者:User

標籤:

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)和結構體的封裝與解鎖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.