手机
当前位置:查字典教程网 >编程开发 >IOS开发 >Objective-C中NSNumber与NSDictionary的用法简介
Objective-C中NSNumber与NSDictionary的用法简介
摘要:NSNumber的常用方法在Objective-c中有int的数据类型,那为什么还要使用数字对象NSNumber?这是因为很多类(如NSAr...

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.....)

包装后取出来的方法如下:

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

下面就拿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);

}

【Objective-C中NSNumber与NSDictionary的用法简介】相关文章:

iOS中sqlite数据库的原生用法

总结iOS中runtime的使用

iOS App开发中UISearchBar搜索栏组件的基本用法整理

iOS应用开发中AFNetworking库的常用HTTP操作方法小结

iOS的UI开发中Button的基本编写方法讲解

详解Objective-C编程中对设计模式中适的配器模式的使用

iOS中UIAlertView警告框组件的使用教程

举例讲解Objective-C中@property属性的用法

IOS开发中NSURL的基本操作及用法详解

Objective-C中字符串的拼接方法小结

精品推荐
分类导航