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是有符号的。

1》、当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。

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

3》、有人说既然都有了NSInteger等这些基础类型了为什么还要有NSNumber?它们的功能当然是不同的。

NSInteger是基础类型,但是NSNumber是一个类。如果想要存储一个数值,直接用NSInteger是不行的,比如在一个Array里面这样用:

NSArray *array= [[NSArray alloc]init];
[array addObject:3];//会编译错误

这样是会引发编译错误的,因为NSArray里面放的需要是一个类,但‘3’不是。这个时候需要用到NSNumber:

NSMutableArray *array= [[NSMutableArray alloc]init];
[array addObject:[NSNumber numberWithInt:3]];

一下两行代码是会有警告的  因为NSArray 是不可变的.

NSArray *array1= [[NSArray alloc]init];

[array1 addObject:[NSNumber numberWithInt:3]];

Cocoa提供了NSNumber类来包装(即以对象形式实现)基本数据类型。

例如以下创建方法:

+ (NSNumber*)numberWithChar: (char)value;
+ (NSNumber*)numberWithInt: (int)value;
+ (NSNumber*)numberWithFloat: (float)value;
+ (NSNumber*)numberWithBool: (BOOL) value;

将基本类型数据封装到NSNumber中后,就可以通过下面的实例方法重新获取它:

- (char)charValue;
- (int)intValue;
- (float)floatValue;
- (BOOL)boolValue;
- (NSString*)stringValue;

参考:

http://www.wuleilei.com/blog/335

http://blog.csdn.net/weasleyqi/article/details/33396809

iOS开发之int,NSInteger,NSUInteger,NSNumber的使用的更多相关文章

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

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

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

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

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

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

  4. iOS开发之Socket通信实战--Request请求数据包编码模块

    实际上在iOS很多应用开发中,大部分用的网络通信都是http/https协议,除非有特殊的需求会用到Socket网络协议进行网络数 据传输,这时候在iOS客户端就需要很好的第三方CocoaAsyncS ...

  5. iOS开发之UISearchBar初探

    iOS开发之UISearchBar初探 UISearchBar也是iOS开发常用控件之一,点进去看看里面的属性barStyle.text.placeholder等等.但是这些属性显然不足矣满足我们的开 ...

  6. iOS 开发之Block

    iOS 开发之Block 一:什么是Block.Block的作用 UI开发和网络常见功能的实现回调,按钮事件的处理方法是回调方法. 1.     按钮事件 target action 机制. 它是将一 ...

  7. iOS开发之Xcode常用调试技巧总结

    转载自:iOS开发之Xcode常用调试技巧总结 最近在面试,面试过程中问到了一些Xcode常用的调试技巧问题.平常开发过程中用的还挺顺手的,但你要突然让我说,确实一脸懵逼.Debug的技巧很多,比如最 ...

  8. 李洪强iOS开发之Block和协议

    李洪强iOS开发之Block和协议 OC语言BLOCK和协议 一.BOLCK (一)简介 BLOCK是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任 ...

  9. iOS 开发之 GCD 不同场景使用

    header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...

随机推荐

  1. 认识Cookie和状态管理

    HTTP协议是一种无状态的协议,WEB服务器本身不能识别出哪些请求是同一个浏览器发出的 ,浏览器的每一次请求都是完全孤立的 即使 HTTP1.1 支持持续连接,但当用户有一段时间没有提交请求,连接也会 ...

  2. WebBrowser中运行js

    HtmlElement script = wf.WebBrowser.Document.CreateElement("script"); script.SetAttribute(& ...

  3. 1、CentOS 6 安装GitLab

    1.安装和配置必需的依赖项 在CentOS上将系统防火墙打开HTTP和SSH访问. sudo yum install -y curl policycoreutils-python openssh-se ...

  4. 【转载】Beautiful Soup库(bs4)入门

    转载自:Beautiful Soup库(bs4)入门 该库能够解析HTML和XML 使用Beautiful Soup库:      from bs4 import BeautifulSoup impo ...

  5. SQL Server 数据库优化剖析

    一.SQL Profiler 事件类 Stored Procedures\RPC:Completed TSQL\SQL:BatchCompleted 事件关键字段 EventSequence.Even ...

  6. numpy 练习

    numpy学习,为后续机器学习铺垫 参考网址 #!/usr/bin/python #coding=utf-8 #__author__='dahu' # from numpy import * impo ...

  7. 报错org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"

    org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n ...

  8. windows下怎样使用md命令一次建立多级子目录

    在Windows系统中一次只能够创建一个子目录,在命令提示符窗口则可以一次性创建多个子目录,例如如果想在f盘创建多级子目录,则md 23\13\65\45,后面的数字随便都可以.如果想一次性删除多级目 ...

  9. ref:JAVA代码审计的一些Tips(附脚本)

    ref:https://xz.aliyun.com/t/1633/ JAVA代码审计的一些Tips(附脚本) 概述 本文重点介绍JAVA安全编码与代码审计基础知识,会以漏洞及安全编码示例的方式介绍JA ...

  10. ref:Adding AFL Bloom Filter to Domato for Fun

    ref:https://www.sigpwn.io/blog/2018/5/13/adding-afl-bloom-filter-to-domato-for-fun Adding AFL Bloom ...