版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/weasleyqi/article/details/33396809

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

+ 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]];

写的比較简单,希望有帮助。

【iOS知识学习】_int、NSInteger、NSUInteger、NSNumber的差别和联系的更多相关文章

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

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

  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.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备 ...

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

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

  6. 【iOS知识学习】_UITableView简介

    UITableView在iOS中估计是用的最多的控件了吧,几乎每个app都会用到. 一.它是一个非常重要的类来在table中展示数据. 1.是一个一维的表: 2.是UIScrollView的一个子类: ...

  7. 【转】【iOS知识学习】_视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear等的区别及用途

    原文网址:http://blog.csdn.net/weasleyqi/article/details/8090373 iOS视图控制对象生命周期-init.viewDidLoad.viewWillA ...

  8. 【iOS知识学习】_iOS沙盒机制

    IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序仅仅能在为该应用创建的目录内读取文件,不能够訪问其它地方的内容.全部的非代码文件都保存在这个地方.比方图片.声音.属性列表和文本文件 ...

  9. 【iOS知识学习】_如何判断手机是否为静音模式

    目前我涉及的app要获取系统是否为静音模式,到网上搜了一下,千篇一律的都是一样的,而且都是iOS5.0以前才适应的知识,这个大家去搜一下就会很容易发现,找了很久终于找到一个5.0以后适用的,那位大神的 ...

随机推荐

  1. Python学习笔记(六)Python组合数据类型

    在之前我们学会了数字类型,包括整数类型.浮点类型和复数类型,这些类型仅能表示一个数据,这种表示单一数据的类型称为基本数据类型.然而,实际计算中却存在大量同时处理多个数据的情况,这种需要将多个数据有效组 ...

  2. bzoj3064/洛谷P4314 CPU监控【线段树】

    好,长草博客被催更了[?] 我感觉这题完全可以当作线段树3 线段树2考加法和乘法标记的下放顺序,这道题更丧心病狂[?] 很多人可能跟我一样,刚看到这道题秒出思路:打一个当前最大值一个历史最大值不就完事 ...

  3. Oracle删除当前用户下所有的表的方法1

    1.如果有plsql客户端,则可以使用该用户登录,选中所有表 右键Drop即可. 2.如果有删除用户的权限,则可以: ? 1 drop user user_name cascade; 加了cascad ...

  4. DLedger —基于 raft 协议的 commitlog 存储库

    “点击获取上云帮助文档” 尊敬的阿里云用户: 您好!为方便您试用开源 RocketMQ 客户端访问阿里云MQ,我们申请了专门的优惠券,优惠券可以直接抵扣金额.请填写下您公司账号信息,点击上图,了解更多 ...

  5. MaxCompute Spark开发指南

    0. 概述 本文档面向需要使用MaxCompute Spark进行开发的用户使用.本指南主要适用于具备有Spark开发经验的开发人员. MaxCompute Spark是MaxCompute提供的兼容 ...

  6. Vue--vue中常用的ECMAScript6语法

    1.对象的写法 es5中对象: {add:add,substrict:substrict} es6中对象: {add,substrict} 注意这种写法的属性名称和值变量是同一个名称才可以简写,否则要 ...

  7. LintCode_469 等价二叉树

    题目 检查两棵二叉树是否等价.等价的意思是说,首先两棵二叉树必须拥有相同的结构,并且每个对应位置上的节点上的数都相等. 样例 1 1 / \ / \ 2 2 and 2 2 / / 4 4 就是两棵等 ...

  8. Android——app基础

    Android Application基础 系统启动过程 APK文件介绍 APK是Android Package的缩写,即android安装包.APK 文件其实是zip 格式,但后缀名被修改为apk  ...

  9. No.6 Verilog 其他论题

    (1)任务  **任务类似于一段程序,可以提供一种能力,使设计者可以从设计描述的不同位置执行共同的代码段.任务可以包含时序控制, 可以调用其它任务和函数.  任务的定义格式: task[automat ...

  10. win10下安装mongodb(解压版)

    首先到官网下载安装包.(https://www.mongodb.com/download-center#community) 1.创建mongodb目录 2.配置文件mongodb.config 3. ...