1、首先先了解下NSNumber类型:

苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html

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对象有以下方法:

  1. + numberWithBool:
  2. + numberWithChar:
  3. + numberWithDouble:
  4. + numberWithFloat:
  5. + numberWithInt:
  6. + numberWithInteger:
  7. + numberWithLong:
  8. + numberWithLongLong:
  9. + numberWithShort:
  10. + numberWithUnsignedChar:
  11. + numberWithUnsignedInt:
  12. + numberWithUnsignedInteger:
  13. + numberWithUnsignedLong:
  14. + numberWithUnsignedLongLong:
  15. + numberWithUnsignedShort:

初始化方法:

  1. – initWithBool:
  2. – initWithChar:
  3. – initWithDouble:
  4. – initWithFloat:
  5. – initWithInt:
  6. – initWithInteger:
  7. – initWithLong:
  8. – initWithLongLong:
  9. – initWithShort:
  10. – initWithUnsignedChar:
  11. – initWithUnsignedInt:
  12. – initWithUnsignedInteger:
  13. – initWithUnsignedLong:
  14. – initWithUnsignedLongLong:
  15. – initWithUnsignedShort:

检索

  1. – boolValue
  2. – charValue
  3. – decimalValue
  4. – doubleValue
  5. – floatValue
  6. – intValue
  7. – integerValue
  8. – longLongValue
  9. – longValue
  10. – shortValue
  11. – unsignedCharValue
  12. – unsignedIntegerValue
  13. – unsignedIntValue
  14. – unsignedLongLongValue
  15. – unsignedLongValue
  16. – unsignedShortValue

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

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

格式:

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

  1. intNumber = [NSNumber numberWithInt:100];
  2. longNumber = [NSNumber numberWithLong:0xabcdef];
  3. floatNumber = [NSNumber numberWithFloat:10.01];

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

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

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

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

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

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

  1. NSArray *array = [NSArray alloc] init];
  2. [array addObject:[NSNumber numberWithInt:3]];

写的比较简单,希望有帮助

_int、NSInteger、NSUInteger、NSNumber的区别和联系的更多相关文章

  1. int, NSInteger, NSUInteger, NSNumber的区别

    新手在接触iOS或者Mac开发的时候,看到int和NSInteger,一般不清楚应该用哪个比较合适.我们先来查看下NSInteger的定义 #if __LP64__ || (TARGET_OS_EMB ...

  2. object-c中的int NSInteger NSUInteger NSNumber辨析

    object-c中的int NSInteger NSUInteger NSNumber辨析 #import <Foundation/Foundation.h> int main(int a ...

  3. 数字类型(NSInteger,NSUInteger,NSNumber)

    在Objective-C中,我们可以使用c中的数字数据类型,int.float.long等.它们都是基本数据类型,而不是对象.也就是说,不能够向它们发送消息.然后,有些时候需要将这些值作为对象使用. ...

  4. iOS开发之int,NSInteger,NSUInteger,NSNumber的使用

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

  5. ios开发 int,NSInteger,NSUInteger,NSNumber

    分享一下,在工作工程中遇到的一些不留心的地方: 1.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备 ...

  6. 85、int 、NSInteger、NSUInteger、NSNumber的区别和联系

    NSNumber是NSValue的一个子类,它是一个对象来存储数字值包括bool型,它提供了一系列的方法来存储char a signed or unsigned char, short int, in ...

  7. IOS基础之 (九) Foundation框架

    一NSNumber 类型转换 NSNumber 把基本数据类型包装成一个对象类型.NSNumber之所以可以(只能)包装基本数据类型,是因为继承了NSValue. @20 等价于 [NSNumber ...

  8. Objective-C中的基本数据类型

    // // main.m // 01.基本数据类型 // // Created by zhangqs008 on 14-2-13. // Copyright (c) 2014年 zhangqs008. ...

  9. IOS Number 处理(int-->NSNumber,NSNumber-->nsinteger,string -->double,CGFloat --> dobule)

    1 小结: 1)int-->NSNumber:numberWithInt 2)NSNumber-->nsinteger:integerValue 3)string -->double ...

随机推荐

  1. 使用JS创建表格以及隔行换色(包括隔N行换色)

    <html> <head> <title></title> <style> table{ width:800px; border-colla ...

  2. POJ3259——Wormholes(Bellman-Ford+SPFA)

    Wormholes DescriptionWhile exploring his many farms, Farmer John has discovered a number of amazing ...

  3. 23.allegro中钻孔[原创]

    1.钻孔: NC Parameters,NC Drill, Dill Legend, NC Route ----------------------- ------ ----- ------- --- ...

  4. 【HDOJ】3386 Final Kichiku “Lanlanshu”

    数位DP.需要注意的是需要特殊处理前导0,另外连续的==匹配,不要计重了,尽量贪心的匹配掉. /* 3886 */ #include <iostream> #include <sst ...

  5. linux 查看系统信息命令

    linux 查看系统信息命令是linux初学者必备的基础知识, 这些命令也非常有用, 因为进入linux第一件事就可能是首先查看系统信息, 因此必要的系统的学习一下这些linux系统信息命令还是非常有 ...

  6. 谈谈javascript插件的写法

    插件顾名思义就是能在一个页面多处使用, 各自按自己的参数配置运行, 并且相互不会冲突. 会写javascript插件是进阶js高级的必经之路, 也是自己所学知识的一个典型的综合运用. 如果你还没头绪, ...

  7. 什么是WebService

    举个例子:现在有5个项目,项目彼此独立,甚至都不是同一类语言进行开发的.这5个项目是:百度知道,百度贴吧,百度新闻,百度视频,百度百科.突然有一天,老板说:把这几个系统揉称一个大项目,起名直接叫做百度 ...

  8. poj3254

    还是那句老话:dp关键在状态: 求有多少种排布方式,是任意两头牛不相邻(有些地方不能放): 不用心,一开始还纠结了半天 和之前USACO上某题方法是一样的,每一行放或不放只有两种情况 把它当作一个二进 ...

  9. [原]Unity3D深入浅出 - 脚本开发基础(Scripts)

    常用脚本事件: Update:每帧调用一次 Start:在第一次Update执行前调用 Awake:脚本实例在创建时调用 FixedUpdate:每个固定物理时间间隔调用一次 LateUpdate:每 ...

  10. [swustoj 917] K-lucky-number

    K-lucky-number(0917) 问题描述 K-lucky-number is defined as add up the number of each bit is a multiple o ...