zoukankan      html  css  js  c++  java
  • 【iOS知识学习】_int、NSInteger、NSUInteger、NSNumber的差别和联系

    版权声明:本文为博主原创文章。未经博主同意不得转载。 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]];

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










查看全文
  • 相关阅读:
    hdu 4027 Can you answer these queries? 线段树
    ZOJ1610 Count the Colors 线段树
    poj 2528 Mayor's posters 离散化 线段树
    hdu 1599 find the mincost route floyd求最小环
    POJ 2686 Traveling by Stagecoach 状压DP
    POJ 1990 MooFest 树状数组
    POJ 2955 Brackets 区间DP
    lightoj 1422 Halloween Costumes 区间DP
    模板 有源汇上下界最小流 loj117
    模板 有源汇上下界最大流 loj116
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10547501.html
  • Copyright © 2011-2022 走看看