object-c基本知识可以参考:基本介绍
十分钟让你明白Objective-C的语法(和Java、C++的对比)
有了以上基础知识,就可以学习oc的基本数据类型。
oc是c语言的超集,当然支持所有的c语言数据类型,就不在说了。
下面说说oc的特有类型:
NSNumber
官方介绍:
NSNumber
is a subclass of
NSValue
that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned
char
,
short int
,
int
,
long int
,
long long int
,
float
, or
double
or as a
BOOL
. (Note that number objects do not necessarily preserve the type they are created with.) It also defines a
compare:
method to determine the ordering of two
NSNumber
objects.
NSNumber
is “toll-free bridged” with its Core Foundation counterpart, CFNumberRef
. See “Toll-Free Bridging” for more information on toll-free bridging.
大致意思是对c语言基本数据类型的封装,方便和Core Foundation对接。
例子:
NSNumber *intNum = [NSNumber numberWithInt:4];
NSLog(@"intNum:%d",[intNum intValue]); // print 4
//其他数据类型可以参考api
/**
Creating an NSNumber Object
+ numberWithBool:
+ numberWithChar:
+ numberWithDouble:
+ numberWithFloat:
+ numberWithInt:
+ numberWithInteger:
+ numberWithLong:
+ numberWithLongLong:
+ numberWithShort:
+ numberWithUnsignedChar:
+ numberWithUnsignedInt:
+ numberWithUnsignedInteger:
+ numberWithUnsignedLong:
+ numberWithUnsignedLongLong:
+ numberWithUnsignedShort:
*/
//另外Effective Object-c 2.0 第三条指出我们多用字面量语法,少用与之等价的方法。
NSNumber *intNum2 = @1;
NSLog(@"intNum2:%d",[intNum2 intValue]); //print 1
iOS开发之int,NSInteger,NSUInteger,NSNumber的使用