先简述下关于NSNumber的信息

  • NSNumber的存在就相当于java中的装箱与拆箱。只不过java中的装箱拆箱过程,使用的是对应的类型,比如基本数据类型是int、double类型,装箱时就得对应使用Integer、Double类型。而Objective-C中,使用的都是NSNumer类型。也因此NSNumber其实是一个类簇,而不是一个类。
  • 在Objective-C编程中,常常是需要将基本数据类型转换为对象来使用的。比如,NSArray、NSDictionary中只能放id类型(即对象类型)。服务器返回的JSON数据解析后是包含的是NSNumber而不是基本数据类型。
  • NSNumber是继承于NSValue。
  • 正是因为NSNumber是一个类簇,会导致使用NSNumber装箱BOOL类型,后面却拆箱成NSInteger类型。虽然基本数据类型之间是有一套相互转换的机制的,但是为了方便省事,建议就不要进行骚操作了吧。

接下来主要就是细致化的把对应的封装、拆箱的方法罗列出来。

一、创建一个NSNumber对象(装箱)

// char
+ (NSNumber *)numberWithChar:(char)value; // 无符号char
+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value; // short
+ (NSNumber *)numberWithShort:(short)value; // 无符号short
+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value; // int
+ (NSNumber *)numberWithInt:(int)value; // 无符号int
+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value; // long
+ (NSNumber *)numberWithLong:(long)value; // 无符号long
+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value; // long long
+ (NSNumber *)numberWithLongLong:(long long)value; // 无符号long long
+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value; // float
+ (NSNumber *)numberWithFloat:(float)value; // double
+ (NSNumber *)numberWithDouble:(double)value; // BOOL
+ (NSNumber *)numberWithBool:(BOOL)value; // NSInteger
+ (NSNumber *)numberWithInteger:(NSInteger)value; // NSUInteger
+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value;

二、初始化一个NSNumber对象(装箱)

// char
- (NSNumber *)initWithChar:(char)value; // 无符号char
- (NSNumber *)initWithUnsignedChar:(unsigned char)value; // short
- (NSNumber *)initWithShort:(short)value; // 无符号short
- (NSNumber *)initWithUnsignedShort:(unsigned short)value; // int
- (NSNumber *)initWithInt:(int)value; // 无符号int
- (NSNumber *)initWithUnsignedInt:(unsigned int)value; // long
- (NSNumber *)initWithLong:(long)value; // 无符号long
- (NSNumber *)initWithUnsignedLong:(unsigned long)value; // long long
- (NSNumber *)initWithLongLong:(long long)value; // 无符号long long
- (NSNumber *)initWithUnsignedLongLong:(unsigned long long)value; // float
- (NSNumber *)initWithFloat:(float)value; // double
- (NSNumber *)initWithDouble:(double)value; // BOOL
- (NSNumber *)initWithBool:(BOOL)value; // NSInteger
- (NSNumber *)initWithInteger:(NSInteger)value; // NSUInteger
- (NSNumber *)initWithUnsignedInteger:(NSUInteger)value;

三、获取NSNumber中的基础数据类型(拆箱)

// char
@property (readonly) char charValue; // 无符号char
@property (readonly) unsigned char unsignedCharValue; // short
@property (readonly) short shortValue; // 无符号short
@property (readonly) unsigned short unsignedShortValue; // int
@property (readonly) int intValue; // 无符号int
@property (readonly) unsigned int unsignedIntValue; // long
@property (readonly) long longValue; // 无符号long
@property (readonly) unsigned long unsignedLongValue; // long long
@property (readonly) long long longLongValue; // 无符号long long
@property (readonly) unsigned long long unsignedLongLongValue; // float
@property (readonly) float floatValue; // double
@property (readonly) double doubleValue; // BOOL
@property (readonly) BOOL boolValue; // NSInteger
@property (readonly) NSInteger integerValue; // NSUInteger
@property (readonly) NSUInteger unsignedIntegerValue;

四、检索字符串表示

NSNumber *intNun = [NSNumber numberWithChar:"c"];
NSLog(@"~~~~~~~~~~%@", [intNun description]);
NSLog(@"~~~~~~~~~~%@", [intNun descriptionWithLocale:nil]);
====打印
~~~~~~~~~~-
~~~~~~~~~~-

五、比较NSNumber对象

typedef NS_ENUM(NSInteger, NSComparisonResult) {
NSOrderedAscending = -1L, //升序(左 < 右)
NSOrderedSame, // 相等(左 = 右)
NSOrderedDescending //降序(左 > 右)
}; // 如果两个NSNumber装箱的基本数据类型不一致。按照标准C的基本数据类型转换后,进行比较
- (NSComparisonResult)compare:(NSNumber *)otherNumber; // 如果是两个数字型的NSNumber对象的比较,用此方法比compare:方法效率更高
- (BOOL)isEqualToNumber:(NSNumber *)number;

标准C语言的基本数据类型介绍,请移步【】

标准C语言的基本数据类型转换规则,请移步【】

数字对象NSNumber的使用的更多相关文章

  1. 数字对象NSNumber

    //将int类型转化成对象 ; NSNumber *numberString = [NSNumber numberWithInt:number]; //对象是可以放入数组的 NSArray *arra ...

  2. 黑马程序员_ Objective-c 之Foundation之NSNumber ,NSValue, NSDate

    Objective-c 之Foundation之NSNumber ,NSValue, NSDate 1.NSNumber具体用法如下: 在Objective-c中有int的数据类型,那为什么还要使用数 ...

  3. NSIntger CGFloat NSNumber

    NSIntger  CGFloat  NSNumber 1.NSIntger  (long) %ld NSInteger a=; NSLog(@"----------%ld",(l ...

  4. NSNumber

    integerfloatc 在Objective-c中有int的数据类型,那为什么还要使用数字对象NSNumber?这是因为很多类(如NSArray)都要求使用对象,而int不是对象.NSNumber ...

  5. oc随笔四:NSString、NSNumber

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...

  6. objective-c常用方法列表(总结)

    第1章 Objective-C学习环境准备 1.1 Objective-C基础 1.1.1 Objective-C的发展历程 1.1.2 Objective-C语言的特点 1.1.3 技术架构 1.2 ...

  7. oc语言的Foundation框架(学习笔记2)

    紧接上文…… 4.集合对象 4.1数组 1.基本概念 Foundation中的数组(NSArray,NSMutableArray)是一组有序的对象集合,通过索引下标获取到数组中的各个元素,也分可变和不 ...

  8. NSNumber、NSValue、NSDate、NSObject

    注:OC中数组和字典只能存储OC对象不能存放基本数据类型. NSNumber NSNumber可以用来把一个基本数据类型包装成一个NSNumber类型的对象. NSNumber *number = [ ...

  9. _int、NSInteger、NSUInteger、NSNumber的区别和联系

    1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/F ...

随机推荐

  1. Java读文件夹

    使用JAVA读取文件夹中的多个文件 package hx.ReadFile; import java.io.FileNotFoundException; import java.io.IOExcept ...

  2. numpy中的max()函数

    1.ndarray.max([int axis]) 函数功能:求ndarray中指定维度的最大值,默认求所有值的最大值. axis=0:求各column的最大值 axis=1:求各row的最大值

  3. 解决Vue 使用vue-router切换页面时 页面显示没有在顶部的问题

    有时候我们需要页面滚动条滚动到某一固定的位置,一般使用Window scrollTo() 方法. 语法就是:scrollTo(xpos,ypos) xpos:必需.要在窗口文档显示区左上角显示的文档的 ...

  4. ElasticSearch学习记录 - 命令示例

    GET /searchfilmcomments/searchfilmcomments/_search { "query": { "match_all": {} ...

  5. C语言-define 与do{}while(0)

    问题引出: 我们都知道宏定义#define只是简单替换,所以遇到复杂的带参数宏,必须很小心的为需要的参数加上括号“()”:同样碰到复杂的多条语句替代,虽然加{}可以将其封装成一个整体,但同时又有另一个 ...

  6. centos7使用docker制作tomcat本地镜像

    1.安装Docker 安装docker前请确认当前linux的内核版必须是3.10及以上 命令: uname  -r 1).yum install -y yum-utils device-mapper ...

  7. 「JSOI2015」最大公约数

    「JSOI2015」最大公约数 传送门 考虑先枚举区间左端点, 然后我们会发现所有可能的区间虽然有 \(O(n)\) 个,但是本质不同的区间 \(\gcd\) 只有 \(\log n\) 级别,而且是 ...

  8. pagehelper 分页不生效,总页数总是1解决方案

    问题: 后台查询后的数据只有1页,已经设置了PageHelper也没用 PageHelper.startPage(pageNum,pageSize); ModelAndView mv=new Mode ...

  9. spring security几大核心组件

    一.SecurityContext 安全上下文,用户通过Spring Security 的校验之后,验证信息存储在SecurityContext中 SecurityContext接口只定义了两个方法, ...

  10. 如何搭建OWASP测试靶机

    刚刚入门的新手都需要一个可以用来练习的环境,但是dvwa的搭建需要相关环境,所以这里推荐大家在虚拟机上搭建owasp靶机,里面集成了dvwa靶机. https://sourceforge.net/pr ...