Objective-C基礎夯實篇——NSValue&NSNumber(裝箱和拆箱)

來源:互聯網
上載者:User

標籤:

概述

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(裝箱和拆箱)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.