标签:
NSNumber和NSValue都是用来做装箱用的,把基本类型的数据装成对象。官方文档给的资料说明地很清楚:
/* NSValue.h Copyright (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主要用于处理我们常用的基本数据类型。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(装箱和拆箱)
标签:
原文地址:http://www.cnblogs.com/hushunfeng/p/4630675.html