您的位置:首页 > 其它

OC中Foundation集合的使用(其中包括基本数据类型与对象进行转换)

2014-12-01 00:19 706 查看
////数据与对象之间的转换

// NSString *temp1 = @"123";

// NSString *temp2 = @"234";

////1、字符串的拼接

// NSString *string = [NSString stringWithFormat:@"%@%@",temp1,temp2];

// NSLog(@"%@",string);

////2、字符串转换成int

// int numInt = [string intValue];

// NSLog(@"%d",numInt);

////3、int转换成字符串

// NSString *intString = [NSString stringWithFormat:@"%d",numInt];

// NSLog(@"%@",intString);

////4、字符串转换为double类型

// double doubleString = [intString doubleValue];

// NSLog(@"%.2lf",doubleString);

//5、NSNumber对象转换为double类型

// NSNumber *num1 = [[NSNumber alloc] initWithDouble:11.1];

// double num = [num1 doubleValue];

// NSLog(@"%.2lf",num);

//NSArray

////创建数组(不可以改变大小的数组)

// NSArray *arr = [NSArray arrayWithObject:@"value1"];

// NSArray *arr = [NSArray arrayWithObjects:@"value1" , @"value2" , @"value3" , @"value4" , nil];

// int counts = [arr count];

// NSString *string1 = [arr objectAtIndex:0];

// NSString *string = [arr componentsJoinedByString:@"->"];

// Boolean result = [arr containsObject:@"value1"];

// int index = [arr indexOfObject:@"value1"];

// NSString *var=[arr objectAtIndex:1];

// NSLog(@"%@",var);

// NSLog(@"%d",index);

// NSLog(@"%d",result);

// NSLog(@"%d",counts);

// NSLog(@"%@",string);

// //通过数组创建数组

// NSArray *arr1 = [NSArray arrayWithArray:arr];

//

// //获取数组中元素的个数

// long int count = [arr count] ;

// NSLog(@"%ld",count);

//

// //访问数组中制定下标的值

// NSString *value1=[arr objectAtIndex:1];

// NSLog(@"%@",value1);

//

// //追加数组给另一个新的数组

// NSArray *arr2 = [arr arrayByAddingObject:@"end"];

// NSLog(@"%@",arr2);

//

// //用指定的字符把数组里面的元素连接起来,用字符串的形式打印出来。

// NSString *string = [arr2 componentsJoinedByString:@","];

// NSLog(@"%@",string);

//

// //判断数组中是否存在指定的对象

// Boolean result = [arr1 containsObject:@"one"];

// if(result){

// NSLog(@"存在");

// }else{

// NSLog(@"不存在");

// }

//

// //数组中根据value寻找下标

// NSInteger *pIndex = [arr1 indexOfObject:@"two"];

// NSLog(@"%ld",pIndex);

//

// //创建一个可以改变大小的数组

// NSMutableArray *arrChange = [NSMutableArray array];//空数组

// NSMutableArray *arrC = [NSMutableArray arrayWithObjects:@"1" , @"2" , @"3" , @"4" , @"5" , nil];

// NSArray *arr = [arrC arrayByAddingObject:@"6"];//添加一个元素并返回一个新的数组

// [arrC insertObject:@"我是新的" atIndex:3];

// [arrC removeObjectAtIndex:3];

// [arrC removeObject:@"1"];

// NSLog(@"%@",arrC);

//

// //数组中添加指定的元素

// [arrChange insertObject:@"ccc" atIndex:0];

// [arrChange insertObject:@"cca" atIndex:1];

// [arrChange insertObject:@"ccb" atIndex:2];

// NSLog(@"%@",arrChange);

//

// //数组中移除指定的元素

// [arrChange removeObjectAtIndex:0];

// NSLog(@"%@",arrChange);

// //数组中移除根据指定的元素

// [arrChange removeObject:@"ccc"];

// NSLog(@"%@",arrChange);

//NSDictionary(一个长度固定的字典)

// NSDictionary *dic = [NSDictionary dictionaryWithObject:@"1" forKey:@"key1"];

// NSLog(@"%@",dic);

// NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"lala" , @"1" , @"demaxiya" , @"2" , nil];//有点反过来了,是值键。

// NSLog(@"%@",dic1);

// int count = [dic1 count];

// NSLog(@"%d",count);

// NSObject *obj = [dic1 objectForKey:@"1"];

// NSArray *arr = [dic1 allKeys];

// NSArray *arr1 = [dic1 allValues];

// NSLog(@"%@",arr1);

// NSLog(@"%@" , obj);

//字典创建的方法

// NSNumber *numObject = [NSNumber numberWithInt:100];

// NSDictionary *dic = [NSDictionary dictionaryWithObject:numObject forKey:@"key"];

// NSLog(@"%@",dic);

// NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key",@"value1",@"key1",@"value2",@"key2",nil];

// NSLog(@"%@",dic1);

// NSDictionary *dic2 = [NSDictionary dictionaryWithDictionary:dic1];

// NSLog(@"%@",dic2);

////获取字典数量

// NSInteger count = [dic1 count];

// NSLog(@"%d",count);

////获取key对应的value对象

// NSObject *valueObj = [dic1 objectForKey:@"key1"];

// NSLog(@"%@",valueObj);

////将字典的key转换成一个枚举对象,用于遍历

// NSEnumerator *enumerator = [dic1 keyEnumerator];

// for ( NSEnumerator *obj in enumerator) {

// NSLog(@"%@ = %@",obj,[dic1 objectForKey:obj]);

// }

////获取所有key集合

// NSArray *keys = [dic1 allKeys];

// NSLog(@"%@",keys);

//创建一个长度可以改变的字典

// NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"demaxiya" , @"1" , "renzaitazai" , @"2" ,nil];

//// NSLog(@"%@",mDic);//这样遍历一个可以改变的字典是不可以的,需要使用java中的foreach

//// for (id obj in mDic) {

//// NSLog(@"%@",[mDic objectForKey:obj]);

//// }

// NSMutableDictionary *mDic2 = [NSMutableDictionary dictionary];

// [mDic2 setDictionary:mDic];//字典的拷贝

// [mDic2 removeObjectForKey:@"1"];//移除指定的元素

// [mDic2 setObject:@"haha" forKey:@"2"];//修改指定的元素

// NSLog(@"%@",[mDic2 objectForKey:@"2"]);

// for (id obj in mDic2) {

// NSLog(@"%@",obj);

// }

//NSSet功能与NSArray功能性质一样,它是跟NSArray|NSDictionary不一样,它是无序集合,并且里面存储的数据没有重复的值,在内存中的存储方式不是连续的。NSSet的效率高于NSArray,因为它用来了一个hash算法。

////创建NSSet(固定大小)

// NSArray *arr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",nil];

// NSSet *set = [NSSet setWithObjects:@"1",@"2",@"3",@"5", nil];

// NSSet *set1 = [NSSet setWithSet:set];

// NSSet *set2 = [NSSet setWithArray:arr];//注意使用数组进行初始化的时候,里面的顺序会改变

// NSLog(@"%@",set2);

// int count = [set2 count];

// NSLog(@"%d",count);

// Boolean result = [set2 containsObject:@"1"];

// NSLog(@"%d",result);

////判断set1与Set2是不是完全匹配

// Boolean result1 = [set1 isEqualToSet:set2];

// NSLog(@"%d",result1);

////判断set1是不是set2的字集

// Boolean result2 = [set1 isSubsetOfSet:set2];

// NSLog(@"%d",result2);

////判断set1与set2中是不是有相同的元素

// Boolean result3 = [set1 intersectsSet:set2];

// NSLog(@"%d",result3);

////创建一个可以改变大小set集合

// NSMutableSet *mSet = [NSMutableSet setWithObjects:@"1",@"2",@"3",@"8", nil];

// NSMutableSet *mSet1 = [NSMutableSet setWithObjects:@"1",@"10", nil];

// [mSet addObject:@"4"];

////把一个数组添加进set集合中

// [mSet addObjectsFromArray:arr];

////结合两个set集合

// [mSet removeObject:@"1"];

// [mSet unionSet:mSet1];

// NSLog(@"%@",mSet);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐