Windows   发布时间:2022-05-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了85、int 、NSInteger、NSUInteger、NSNumber的区别和联系大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

NSnumber是NSValue的一个子类,它是一个对象来存储数字值包括bool型,它提供了一系列的方法来存储char a signed or unsigned char,short int,int,long int,long long int,float,or double or as a BOOL,它提供了一个compare:方法来决定两个NSnumber对象的排序;

创建一个NSnumber对象有以下方法:

+ numberWithBool:
+ numberWithChar: + numberWithDouble: + numberWithFloat: + numberWithInt: + numberWithInteger: + numberWithLong: + numberWithLongLong: + numberWithShort: + numberWithUnsignedChar: + numberWithUnsignedInt: + numberWithUnsignedInteger: + numberWithUnsignedLong: + numberWithUnsignedLongLong: + numberWithUnsignedShort:

初始化方法:

– initWithBool:
– initWithChar:
– initWithDouble:
– initWithFloat:
– initWithInt:
– initWithInteger:
– initWithLong:
– initWithLongLong:
– initWithShort:
– initWithUnsignedChar:
– initWithUnsignedInt:
– initWithUnsignedInteger:
– initWithUnsignedLong:
– initWithUnsignedLongLong:
– initWithUnsignedShort:

检索

– boolValue
– charValue
– decimalValue
– doubleValue
– floatValue
– intValue
– IntegerValue
– longLongValue
– longValue
– shortValue
– unsignedCharValue
– unsignedIntegerValue
– unsignedIntValue
– unsignedLongLongValue
– unsignedLongValue
– unsignedShortValue

NSnumber类型有点类似id类型,对于任何类型的数字对象都能用它来声明,也就是用它来声明数字对象,通过声明,很难判断声明变量是什么数字类型,确定数字对象类型多是在初始化的时候才能确定。

数字对象的创建或者初始化:

格式:

NSnumber 数字对象 = [NSnumber numberWith数字类型:数值];

intnumber = [NSnumber numberWithInt:100]; longnumber = [NSnumber numberWithLong:0xabcdef]; floatnumber = [NSnumber numberWithFloat:10.01];

2、 int、 NSInteger、 NSUInteger、NSnumber之间的区别和联系

int : 当使用int类型定义变量的时候,可以像写C程序一样,用int也可以用NSInteger,推荐使用NSInteger ,因为这样就不用虑设备是32位还是64位了。

NSUInteger是无符号的,即没有负数,NSInteger是有符号的。

NSInteger是基础类型,NSnumber是一个类,如果需要存储一个数值,直接使用NSInteger是不行的,比如在一个数组里使用下面的语句就会报错:

NSArray *array = [NSArray alloc] init];
[array addObject:3];

因为array里应该是一个类,但‘3’不是,所以需要用NSnumber:

NSArray *array = [NSArray alloc] init];
[array addObject:[NSnumber numberWithInt:3]];苹果官方文档地址https://developer.apple.com/documentation/foundation/nsnumber

大佬总结

以上是大佬教程为你收集整理的85、int 、NSInteger、NSUInteger、NSNumber的区别和联系全部内容,希望文章能够帮你解决85、int 、NSInteger、NSUInteger、NSNumber的区别和联系所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: