• Objective-C基础夯实篇——NSValue&NSNumber(装箱和拆箱)


    概述


    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;
    }
    
  • 相关阅读:
    Selenium WebDriver基础操作教程
    C# 运行cmd 并 执行命令语句
    【JAVA习题二】判断101-200之间有多少个素数,并输出所有素数。
    【JAVA习题一】古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一 对兔子,假如兔子都不死,问每个月的兔子总数为多少?
    【JAVA随摘笔记一】进制转换
    【C++初学者自学笔记三】哑元函数、缺省参数、内联函数(模块二,PS:需要用到重载函数)
    【C++初学者自学笔记二】函数重载(模块一)
    【JAVA蓝桥杯】基础练习2 十六进制转十进制
    【JAVA蓝桥杯】基础练习1 十进制转十六进制
    【C++初学者自学笔记一】(把自己刚学到的东西做一下总结,如果有哪些地方不足请留言指正)
  • 原文地址:https://www.cnblogs.com/hushunfeng/p/4630675.html
Copyright © 2020-2023  润新知