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. c缺陷与陷阱笔记-第一章 词法陷阱

    1.运算符的贪心性,匹配最长的运算符,例如 n-->0,从-开始,-是运算符,--是运算符,-->就不是,所以是 n -- > 0,--是 a---b,-是,--是,,---不是,所 ...

  2. EntityFreamwork 读写分离

    http://www.cnblogs.com/cjw0511/p/4391092.html

  3. 【无聊放个模板系列】BZOJ 1597 斜率优化

    STL 双向队列DEQUE版本 #include<cstdio> #include<cstdlib> #include<cstring> #include<i ...

  4. EL表达式对数组、集合操作

    el表达式是通过${key}的方式获取对象中的值.在el表达式中有如下几个隐含的对象,pageScope,requestSope,sessionScope,applicationScope,如果要取$ ...

  5. WebViewJavascriptBridge 原理分析

    WebViewJavascriptBridge 原理分析 网上好多都是在介绍 WebViewJavascriptBridge如何使用,这篇文章就来说说 WebViewJavascriptBridge ...

  6. EXPORT_SYMBOL的作用是什么

    http://www.cnblogs.com/riskyer/p/3221805.html EXPORT_SYMBOL只出现在2.6内核中,在2.4内核默认的非static 函数和变量都会自动 导入到 ...

  7. 网上图书商城项目学习笔记-036工具类之CommonUtils及日期转换器

    1.CommonUtils.java package cn.itcast.commons; import java.util.Map; import java.util.UUID; import or ...

  8. Android:使用ViewPager实现左右滑动切换图片(图上有点点)

    在以下实例的基础上加上点点 Android:使用ViewPager实现左右滑动切换图片 (简单版) 效果预览: 因为要把点点放图片上,所以修改布局为相对布局: <?xml version=&qu ...

  9. Cygwin安装时,选择163的源后出错:Unable to get setup.ini from <http://mirrors.163.com/cygwin/>

    [问题] 折腾: [记录]Cygwin下把make从v3.82换成v3.81 期间,选择了163的源,结果出错: Cygwin Setup Unable to get setup.ini from & ...

  10. The document has been modified outside of Code Composer. Would you like to reload the file

    2013-06-20 10:03:32 烧写过程是合众达给出的文档 problem: I'm new to using Code Composer Studio 3.3 and am having a ...