• IOS 学习笔记 2015-03-20 OC-数值类型


    一 定义 
    在Objective-C中,我们可以使用c中的数字数据类型,int、float、long等。它们都是基本数据类型,而不是对象。也就是说,不能够向它们发送消息。然后,有些时候需要将这些值作为对象使用。
    
    二 关键字
    1 NSInteger  int 包装类型  
    	A 当你不知道程序运行哪种处理器架构时,你最好使用NSInteger,因为在有可能int在32位系统中只是int类型,
    	而在64位系统,int可能变是long型。除非不得不使用int/long型,否则推荐使用NSInteger。
    	B 从上面的定义可以看出NSInteger/NSUInteger是一种动态定义的类型,在不同的设备,不同的架构,
    	有可能是int类型,有可能是long类型。
    2 NSUInteger 是无符号的,即没有负数,NSInteger是有符号的。   
    3 NSNumber 
    	A 专门用来装基础类型的对象,把整型、单精度、双精度、字符型等基础类型存储为对象
    	B NSNumber可以将基本数据类型包装起来,形成一个对象,这样就可以给其发送消息,装入NSArray中
    	C NSInteger和普通数据类型是不能被封装到数组中的
    
    //
    //  main.m
    //  OC-NSNumber
    //
    //  Created by wangtouwang on 15/3/20.
    //  Copyright (c) 2015年 wangtouwang. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // insert code here...
            //创建
            NSNumber *_n1 = [[NSNumber alloc] initWithInt:9];
            NSNumber *_n2 = [[NSNumber alloc] initWithFloat:8.1f];
            NSNumber *_n3 = [[NSNumber alloc] initWithDouble:8.1];
            NSNumber *_n4 = [[NSNumber alloc] initWithChar:'A'];
            NSLog(@"N1=%@ N2=%@ N3=%@ n4=%@",_n1,_n2,_n3,_n4);
            //还原
            // 转换为int
            int n1 = [_n1 intValue];
            //转换为double
            double n3 = [_n3 doubleValue];
            //转换为 float
            float n2 = [_n2 floatValue];
            // 转换为 char
            char n4 = [_n4 charValue];
             NSLog(@"N1=%i N2=%f N3=%f n4=%c",n1,n2,n3,n4);
            //比较是否相等
            NSLog(@"%@",[_n3 isEqualToNumber:_n2]?@"==":@"!=");
            //比较大小
             NSLog(@"%ld",[_n1 compare:_n2]);
            
            //数组可以增加NSNumber对象
            NSMutableArray *_array2 = [[NSMutableArray alloc] initWithObjects:_n1,_n2,_n3,_n4, nil];
            NSInteger INDEX= 12;
            //尝试用数组增加NSInteger对象 结果报错了,说明语法不支持
    //         NSMutableArray *_array1 = [[NSMutableArray alloc] initWithObjects:_n1,_n2,_n3,_n4, INDEX,nil];
    //        NSLog(@"%lu",[_array1 count]);
        }
        return 0;
    }
  • 相关阅读:
    switch_goto
    隐藏 窗口的整个 标题栏(包括右上角的关闭)
    asp的邦定表达式异常 <_ %_ = strParentid _%_>不能传到下个页面
    【收藏】default.rdp配置
    计算机网络中的性能指标
    当某个快捷键不能用时很可能是热键冲突
    java的FOR循环 打印三角形
    二进制 八进制 十进制 十六进制
    linux的vim编辑命令常用
    JIRA的备份
  • 原文地址:https://www.cnblogs.com/ak23173969/p/4354016.html
Copyright © 2020-2023  润新知