標籤:
概述
NSNumber和NSValue都是用來做裝箱用的,把基本類型的資料裝成對象。官方文檔給的資料說明地很清楚:
/*NSValue.hCopyright (c) 1994-2014, Apple Inc. All rights reserved.*/#import <Foundation/NSObject.h>@class NSString, NSDictionary;@interface NSValue : NSObject <NSCopying, NSSecureCoding>- (void)getValue:(void *)value;@property (readonly) const char *objCType NS_RETURNS_INNER_POINTER;- (instancetype)initWithBytes:(const void *)value objCType:(const char *)type NS_DESIGNATED_INITIALIZER;- (instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;@end@interface NSValue (NSValueCreation)+ (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type;+ (NSValue *)value:(const void *)value withObjCType:(const char *)type;@end@interface NSValue (NSValueExtensionMethods)+ (NSValue *)valueWithNonretainedObject:(id)anObject;@property (nonatomic, readonly) id nonretainedObjectValue;+ (NSValue *)valueWithPointer:(const void *)pointer;- (void *)pointerValue;- (BOOL)isEqualToValue:(NSValue *)value;@end@interface NSNumber : NSValue- (instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithChar:(char)value NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithUnsignedChar:(unsigned char)value NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithShort:(short)value NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithUnsignedShort:(unsigned short)value NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithInt:(int)value NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithUnsignedInt:(unsigned int)value NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithLong:(long)value NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithUnsignedLong:(unsigned long)value NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithLongLong:(long long)value NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithUnsignedLongLong:(unsigned long long)value NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithFloat:(float)value NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithDouble:(double)value NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithBool:(BOOL)value NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0) NS_DESIGNATED_INITIALIZER;- (NSNumber *)initWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0) NS_DESIGNATED_INITIALIZER;@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;- (NSComparisonResult)compare:(NSNumber *)otherNumber;- (BOOL)isEqualToNumber:(NSNumber *)number;- (NSString *)descriptionWithLocale:(id)locale;@end@interface NSNumber (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);@end
本文主要內容如下:
1.文檔說明
2.測試程式
文檔說明
NSNumber是繼承自NSValue的。NSValue可以裝任意值,NSNumber主要用於處理我們常用的基礎資料型別 (Elementary Data Type)。NSValue的用法還需要待學習深入。
NSNumber有快速文法:使用@ 比如@YES 就可以封裝成對象了。具體在測試程式中有說明。
測試程式
//// main.m// NSNumber&NSValue//// Created by hushunfengMac-CMCC on 15/7/8.// Copyright (c) 2015年 hushunfengMac-CMCC. All rights reserved.//#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSNumber *intNum = [NSNumber numberWithInt:10]; int i = intNum.intValue; NSLog(@"%d",i); //快速文法 NSNumber *boolNum = @YES; BOOL x = boolNum.boolValue; BOOL y = [boolNum boolValue]; NSLog(@"%i",x); NSLog(@"%i",y); //NSLog(@"Hello, World!"); } return 0;}
Objective-C基礎夯實篇——NSValue&NSNumber(裝箱和拆箱)