_int、NSInteger、NSUInteger、NSNumber的区别和联系
1、首先先了解下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]];
写的比较简单,希望有帮助
_int、NSInteger、NSUInteger、NSNumber的区别和联系的更多相关文章
- int, NSInteger, NSUInteger, NSNumber的区别
新手在接触iOS或者Mac开发的时候,看到int和NSInteger,一般不清楚应该用哪个比较合适.我们先来查看下NSInteger的定义 #if __LP64__ || (TARGET_OS_EMB ...
- object-c中的int NSInteger NSUInteger NSNumber辨析
object-c中的int NSInteger NSUInteger NSNumber辨析 #import <Foundation/Foundation.h> int main(int a ...
- 数字类型(NSInteger,NSUInteger,NSNumber)
在Objective-C中,我们可以使用c中的数字数据类型,int.float.long等.它们都是基本数据类型,而不是对象.也就是说,不能够向它们发送消息.然后,有些时候需要将这些值作为对象使用. ...
- iOS开发之int,NSInteger,NSUInteger,NSNumber的使用
1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/F ...
- ios开发 int,NSInteger,NSUInteger,NSNumber
分享一下,在工作工程中遇到的一些不留心的地方: 1.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备 ...
- 85、int 、NSInteger、NSUInteger、NSNumber的区别和联系
NSNumber是NSValue的一个子类,它是一个对象来存储数字值包括bool型,它提供了一系列的方法来存储char a signed or unsigned char, short int, in ...
- IOS基础之 (九) Foundation框架
一NSNumber 类型转换 NSNumber 把基本数据类型包装成一个对象类型.NSNumber之所以可以(只能)包装基本数据类型,是因为继承了NSValue. @20 等价于 [NSNumber ...
- Objective-C中的基本数据类型
// // main.m // 01.基本数据类型 // // Created by zhangqs008 on 14-2-13. // Copyright (c) 2014年 zhangqs008. ...
- IOS Number 处理(int-->NSNumber,NSNumber-->nsinteger,string -->double,CGFloat --> dobule)
1 小结: 1)int-->NSNumber:numberWithInt 2)NSNumber-->nsinteger:integerValue 3)string -->double ...
随机推荐
- 【无聊放个模板系列】POJ 3678 2-SAT
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...
- [itint5]判断是否为二叉搜索树
http://www.itint5.com/oj/#25 这题在leetcode上是用中序遍历来做的,但是这里由于有相等的情况,即左子树小于等于根,这样中序遍历无法完全判定.可以用递归来做,用递归给出 ...
- Hibernate逍遥游记-第1章-JDBC访问数据库
1. package mypack; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.sw ...
- 组策略限制添加用户作为服务登录导致ITAtomcat服务无法启动(log on as a service)
[故障类型]:ITA tomcat服务器无法启动. [关 键 词]:Logon as a service 作为服务登录 tomcat loggeter [适用版本]:FusionCloud So ...
- Android:控件布局(绝对布局)AbsoluteLayout
绝对布局也叫坐标布局,指定元素的绝对位置,因为适应性很差,一般很少用到.可以使用RelativeLayout替代. 常用属性: android:layout_x --------组件x坐标 andr ...
- 安装Hadoop系列 — eclipse plugin插件编译安装配置
[一].环境参数 eclipse-java-kepler-SR2-linux-gtk-x86_64.tar.gz //现在改为eclipse-jee-kepler-SR2-linux-gtk-x86_ ...
- NPAPI插件开发
1.插件是什么 插件是一种遵循一定规范的应用程序接口编写出来的程序.插件必须依附于一个宿主程序,为宿主程序提供增强功能.插件的种类有很多,这里主要讨论浏览器插件. IE下利用OLE和COM技术开发的浏 ...
- POJ1182
这题需要注意就是 并查集中 相对位置 注意与绝对距离区别 #include<cstdio> #define maxn 50005 int ans,i,a,b,p,fa,fb,n,k; in ...
- [swustoj 856] Huge Tree
Huge Tree(0856) 问题描述 There are N trees in a forest. At first, each tree contains only one node as it ...
- 我的WCF之旅(1):创建一个简单的WCF程序
为了使读者对基于WCF的编程模型有一个直观的映像,我将带领读者一步一步地创建一个完整的WCF应用.本应用功能虽然简单,但它涵盖了一个完整WCF应用的基本结构.对那些对WCF不是很了解的读者来说,这个例 ...