当前位置: 移动技术网 > 移动技术>移动开发>IOS > Objective-C中NSNumber与NSDictionary的用法简介

Objective-C中NSNumber与NSDictionary的用法简介

2019年07月24日  | 移动技术网移动技术  | 我要评论
nsnumber的常用方法 在objective-c中有int的数据类型,那为什么还要使用数字对象nsnumber?这是因为很多类(如nsarray)都要求使用对象,而i

nsnumber的常用方法

在objective-c中有int的数据类型,那为什么还要使用数字对象nsnumber?这是因为很多类(如nsarray)都要求使用对象,而int不是对象。
nsnumber就是数字对象我们可以使用nsnumber对象来创建和初始化不同类型的数字对象。
nsnumber
   + (nsnumber *)numberwithint:(int)value;
   + (nsnumber *)numberwithdouble:(double)value;
   - (int)intvalue;
   - (double)doublevalue;
   .....................(对于每个基本类型,类方法都为这它分配了一个nsnumber对象,并将其设置为指定的值,这些方法都是以numberwith开始的,之后是类型,如numberwithfloat,numberwithlong,numberwithinteger.....)
包装后取出来的方法如下:

下面就拿int做个demo:

复制代码 代码如下:

void number() { 
    // 将int类型的10 包装成 一个nsnumber对象 
    nsnumber *number = [nsnumber numberwithint:10]; 
    nslog(@"number=%@", number); 
     
    nsmutablearray *array = [nsmutablearray array]; 
    // 添加数值到数组中 
    [array addobject:number]; 
     
    // 取出来还是一个nsnumber对象,不支持自动解包(也就是不会自动转化为int类型) 
    nsnumber *number1 = [array lastobject]; 
     
    // 将nsnumber转化成int类型 
    int num = [number1 intvalue]; 
    nslog(@"num=%i", num); 

nsdictionary一些常用用法

复制代码 代码如下:

nsarray * skyaarrays = [nsarray arraywithobjects:@"a天空1号",@"a天空2号",@"a天空3号",nil];
    nsarray * skybarrays = [nsarray arraywithobjects:@"b天空1号",@"b天空2号",@"b天空3号",nil];
    nsarray * skycarrays = [nsarray arraywithobjects:@"c天空1号",@"c天空2号",@"c天空3号",nil];
   
    // nsarray * skyarray = [nsarray arraywithobjects:skyaarrays,skybarrays,skycarrays, nil];
   
   //字典中所有的key
    nsarray * keys = [nsarray arraywithobjects:@"name",@"sex",@"age",nil];
   //字典中所有跟key对应的value
    nsarray * values = [nsarray arraywithobjects:@"liuhui",@"男",[nsnumbernumberwithint:36],nil];
   //创建字典对象方法1
    nsdictionary * mydic = [[nsdictionary alloc]initwithobjects:values forkeys:keys];
    nslog(@"my dic = %@",mydic);
   // 创建字典对象方法2    
    nsdictionary * yourdic = [[nsdictionary alloc] initwithobjectsandkeys:skyaarrays,@"a",skybarrays,@"b",skycarrays,@"c",nil];
    nslog(@"your dic = %@",yourdic);
    
    nslog(@"%@",[yourdic objectforkey:@"a"]);
    // - (nsarray *)allkeys; 返回的是 nsarray类型,方便用 objectatindex取出一个个key
    nslog(@"%@",[yourdic allkeys]);
    nslog(@"%@",[yourdic allvalues]);
   
   //添加数据(setobject 一般没有一种key才添加,有同名的key而用这种方法,会覆盖掉),注意:id key  是成对出现的 
    [mutabledictionary setobject:@"good lucky"forkey:@"why"]; 
    [mutabledictionary setobject:@"bye bye" forkey:@"how"]; 


   //删除指定键值的数据 
    [mutabledictionary removeobjectforkey:..]; 
   //删除所有数据 
    [mutabledictionary removeallobjects]; 

    //字典的普通遍历(无序)
    for (int i =0; i < [yourdic count]; i++) {    
        nslog(@"key = value <====> %@ = %@",[[yourdic allkeys] objectatindex:i],[yourdic objectforkey:[[yourdic allkeys]objectatindex:i]]);
    }
   
    // 字典的快速遍历 取出来的obj一定是key
    for (id obj in yourdic) {   
        nslog(@"%@",obj);
        id value = [yourdic objectforkey:obj];  
        nslog(@"%@",value);
    }

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网