资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Objective-C之数字对象-创新互联

int , float , long都是OC的基本数据类型,但是(!important)它们都不是对象。但是有的时候需要将他们最为一个对象来使用,例如:NSArray要求存储的值必须是对象。那么这里就可以使用NSNumber类。
一 , 为NSNumber赋值:
① : 赋值一个int类型的值,创建和初始化 int2O = [NSNumber numberWithInteger:100]
意义: 为int2O赋值×××100对象
②:获得init2O的的值 init2Get = [init2O integerValue]
注意 : integerValue说明init2O里面存的是int类型的值
例如:

创新互联于2013年成立,先为新蔡等服务建站,新蔡等地企业,进行企业商务咨询服务。为新蔡企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSNumber *init2O;
        NSInteger init2Get;
        init2O = [NSNumber numberWithInteger:100];
        init2Get = [init2O integerValue];
        NSLog(@"%li",(long)init2Get);
    }
    return 0;
}

结果:
Objective-C之数字对象

二,可以使用initWithInteger来直接实例化一个NSNumber

Objective-C之数字对象

其他的类型

赋值方法 实例化 检索方法
numberWithCharinitWithCharcharValue
numberWithUnsignedCharinitWithUnsignedCharunsignedCharValue
numberWithShortinitWithShortshortValue
numberWithUnsignedShortinitWithUnsignedShortunsignedShortValue
numberWithIntegerinitWithIntegerintegerValue
numberWithUnsignedIntegerinitWithUnsignedIntegerunsignedIntegerValue
numberWithIntinitWithIntintValue
numberWithUnsignedIntinitWithUnsignedIntunsignedIntValue
numberWithLonginitWithLonglongValue
numberWithUnsignedLonginitWithUnsignedLongunsignedLongValue
numberWithLongLonginitWithLongLonglonglongValue
numberWithUnsignedLongLonginitWithUnsignedLongLongunsignedLongLongValue
numberWithFloatinitWithFloatfloatValue
numberWithDoubleinitWithDoubledoubletValue
numberWithBoolinitWithBoolbooltValue

验证2个number是否是相等的

#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSNumber *init2O = [[NSNumber alloc] initWithInteger:100];
        NSInteger init2Get;
        init2Get = [init2O integerValue];
        NSLog(@"%li",(long)init2Get);
        //验证是否相等
        NSNumber *float2O = [[NSNumber alloc] initWithFloat:100.00];
        if( [init2O isEqualToNumber:float2O] == YES){
            NSLog(@"Equal!!!");
        }
    }
    return 0;
}

结果:
Objective-C之数字对象
验证小于

#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSNumber *init2O = [[NSNumber alloc] initWithInteger:100];
        NSInteger init2Get;
        init2Get = [init2O integerValue];
        NSLog(@"%li",(long)init2Get);
        //验证是否相等
        NSNumber *float2O = [[NSNumber alloc] initWithFloat:100.00];
        if( [init2O compare:float2O] == NSOrderedAscending){
            NSLog(@"Asc!!!");
        }else{
            NSLog(@"No Asc");
        }
    }
    return 0;
}

结果:
Objective-C之数字对象

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章标题:Objective-C之数字对象-创新互联
当前链接:http://cdkjz.cn/article/hgohs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220