数字对象NSNumber的使用
先简述下关于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的使用的更多相关文章
- 数字对象NSNumber
//将int类型转化成对象 ; NSNumber *numberString = [NSNumber numberWithInt:number]; //对象是可以放入数组的 NSArray *arra ...
- 黑马程序员_ Objective-c 之Foundation之NSNumber ,NSValue, NSDate
Objective-c 之Foundation之NSNumber ,NSValue, NSDate 1.NSNumber具体用法如下: 在Objective-c中有int的数据类型,那为什么还要使用数 ...
- NSIntger CGFloat NSNumber
NSIntger CGFloat NSNumber 1.NSIntger (long) %ld NSInteger a=; NSLog(@"----------%ld",(l ...
- NSNumber
integerfloatc 在Objective-c中有int的数据类型,那为什么还要使用数字对象NSNumber?这是因为很多类(如NSArray)都要求使用对象,而int不是对象.NSNumber ...
- oc随笔四:NSString、NSNumber
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...
- objective-c常用方法列表(总结)
第1章 Objective-C学习环境准备 1.1 Objective-C基础 1.1.1 Objective-C的发展历程 1.1.2 Objective-C语言的特点 1.1.3 技术架构 1.2 ...
- oc语言的Foundation框架(学习笔记2)
紧接上文…… 4.集合对象 4.1数组 1.基本概念 Foundation中的数组(NSArray,NSMutableArray)是一组有序的对象集合,通过索引下标获取到数组中的各个元素,也分可变和不 ...
- NSNumber、NSValue、NSDate、NSObject
注:OC中数组和字典只能存储OC对象不能存放基本数据类型. NSNumber NSNumber可以用来把一个基本数据类型包装成一个NSNumber类型的对象. NSNumber *number = [ ...
- _int、NSInteger、NSUInteger、NSNumber的区别和联系
1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/F ...
随机推荐
- Mybatis空指针
查询数据,返回的字段要在 resultMap 中定义, <resultMap id="BaseResultMap" type="com...." > ...
- CentOS安装MySQL的步骤
1.下载 Mysql yum包 http://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/ 下载到本地再上传到服务器,或者使用wget 直接下载 w ...
- 【C语言】(for循环嵌套)找出1000以内的水仙花数
什么是水仙花数? 水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153). 分析: 根据定义可知: a*a*a+b*b*b+c*c*c ...
- 2.1.FastDFS-单机拆分版-调度器安装配置
Centos610系列配置 我们在Centos610FastDFS单机模式-FastDFS安装 中已经完成了FastDFS的安装,接下来我们进行FastDFS调度器的安装. 1.找到FastDFS配置 ...
- 【笔记2-环境配置及初始化】从0开始 独立完成企业级Java电商网站开发(服务端)
准备工作 Linux系统安装 云服务器部署 概要 申请和配置 域名的购买.解析.配置.绑定流程 用户创建实操 环境安装及部署 JDK.Tomcat.Maven下载安装及配置 vsftpd下载安装及配置 ...
- Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析
目录 0.前言 1.TemporalAccessor源码 2.Temporal源码 3.TemporalAdjuster源码 4.ChronoLocalDate源码 5.LocalDate源码 6.总 ...
- 【代码总结】MYSQL数据库的常见操作
============================== MYSQL数据库的常见操作 ============================== 一.mysql的连接与关闭 -h:指定所连接的服 ...
- git清除用户信息
remote: Repository not found. fatal: repository 'https://github.com/chenbowen950908/zhongzanjiaoyu.g ...
- EAP认证
EAP信息交换: 上图中展示的是OTP(一次性密码)实现EAP交换过程,具体的EAP交换过程如下: 步骤1:请求方向认证方发送EAPOL-Start消息,通知对方已经做到了认证准备(注意:若会话由认证 ...
- jQuery结合CSS实现手风琴组件(2)----利用seajs实现静态资源模块化引入
1. 目录结构(webStrom) 2. 代码 1.html <!DOCTYPE html> <html lang="en"> <head> & ...