iOS之Object-c编程基础之NSNumber

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的使用

    



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值