- #import
- int
main(int argc, const char * argv[]) - {
-
-
@autoreleasepool { - //
NSInteger int1 = 123; - //
NSNumber *number1 = [NSNumber numberWithInteger:int1]; - //
NSLog(@"number1:%@",number1); - //
- //
NSInteger int2 = [number1 integerValue]; - //
NSLog(@"int2:%ld",int2); - //
- //
float pi = 3.1415; - //
NSNumber *number2 = [NSNumber numberWithFloat:pi]; - //
- //
float qq = [number2 floatValue]; - //
NSLog(@"QQ:%lf",qq); - //
- #if
0 -
//创建字典 -
-
NSDictionary *dictionary1 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"]; -
-
-
NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAnd Keys:@"v1",@"k1",@"v2",@"k2",@"v3",@"k3",@"v4",@"k4",@"v5",@"k5", nil]; //创建一对【键对值】 -
-
NSDictionary *dictionary3 = [NSDictionary dictionaryWithDictionary :dictionary1]; -
-
-
NSLog(@"dictionary1:%@",dictionary1);//key = value; -
NSLog(@"dictionary2:%@",dictionary2); -
-
NSLog(@"dictionary3:%@",dictionary3);//key = value; -
-
-
//将数组转换为字典 -
NSArray *array1 = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G", nil]; -
NSArray *array2 = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g", nil]; -
NSDictionary *dictionary4 = [NSDictionary dictionaryWithObjects:array1 forKeys:array2]; -
NSLog(@"dictionary4:%@",dictionary4); -
-
//获取字典中的元素个数 -
NSUInteger count = [dictionary4 count]; -
NSLog(@"dictionary4:%ld",count);//dictionary4:7 -
-
//根据key值找出索引的内容 -
NSString *string1 = [dictionary4 objectForKey:@"e"]; -
NSLog(@"the key of dictionary4:%@",string1);//the key of dictionary4:E -
-
NSArray *array3 = [dictionary2 allKeys];//获取字典中所有的key值 -
-
-
NSArray *array4 = [dictionary2 allValues];//获取字典种所有value -
NSLog(@"array3:%@\narray4:%@",array3,array4); -
-
NSLog(@"************************************************************"); -
//转换成枚举,一般用于循环遍历 -
NSEnumerator *enum1 = [dictionary2 keyEnumerator]; -
for (NSString *string2 in enum1) { -
NSLog(@"%@",string2); -
} -
-
//创建一个空的可变字典 - //
NSMutableDictionary *mutableDic1 = [[NSMusicDirectory alloc]init]; - //
NSMutableDictionary *mutableDic2 = [NSMutableDictionary dictionary]; -
-
NSMutableDictionary *mutableDic1 = [NSMutableDictionary dictionaryWithObject:@"A" forKey:@"a"]; -
NSLog(@"添加一个键值对mutableDic1:%@",mutableDic1);//添加一个键值对 -
-
[mutableDic1 setValue:@"b" forKey:@"b"]; -
NSLog(@"%@",mutableDic1); -
-
NSMutableDictionary *mutableDic2 = [NSMutableDictionary dictionaryWithDictionary :dictionary1]; -
[mutableDic2 setDictionary:dictionary1];//可以通过setDictionary方法对一个空的可变字典赋值 -
NSLog(@"对一个空的可变字典赋值%@",mutableDic2); -
-
[mutableDic1 addEntriesFromDictionary :dictionary1]; -
NSLog(@"添加一个字典到可变%@",mutableDic1); -
-
[mutableDic1 removeObjectForKey:@"b"]; -
NSLog(@"删除后的可变字典%@",mutableDic1); -
- #endif
-
NSSet *set1 = [NSSet setWithObjects:@"a",@"b",@"c",@"d", nil]; -
NSLog(@"set1:%@",set1); -
-
NSSet *set2 = [[NSSet alloc]initWithObjects:@"x",@"y",@"z", nil ]; -
NSLog(@"set2:%@",set2); -
-
NSSet *set3 = [NSSet setWithSet:set1]; -
NSLog(@"set3:%@",set3); -
-
NSArray *array =[NSArray arrayWithObjects:@"x",@"y",@"z",@"o",nil]; -
NSSet *set4 = [NSSet setWithArray:array]; -
NSLog(@"%@",set4); -
-
NSLog(@"获取set4中所有元素:%@",[set4 allObjects]); -
NSLog(@"获取set4中某一个元素:%@",[set4 anyObject]); -
-
NSSet *set5 = [set1 setByAddingObject:@"p"]; -
NSSet *set6 = [set1 setByAddingObjectsFromAr ray:array]; -
NSSet* set7 = [set1 setByAddingObjectsFromSe t:set2]; -
-
NSLog(@"将set1里面的所有元素放到set5里面但是没有改变set1里面的元素,set5在初始化的时候就添加了一个元素“p”%@",set5); -
NSLog(@"将数组里面的所有元素放在集合种,但是会把重复的元素去掉%@",set6); -
NSLog(@"根据已有的集合创建集合复制集合产生新的集合%@",set7); -
-
-
BOOL isCon = [set1 containsObject:@"a"]; -
NSLog(@"判断某元素是不是在集合中存在:%d",isCon); -
if (isCon == 1) { -
NSLog(@"found"); -
} -
else -
{ -
NSLog(@"not found"); -
} -
-
NSSet *set8 = [NSSet setWithObjects:@"a",@"b",@"c",@"1", nil]; -
NSSet *set9 = [NSSet setWithObjects:@"a",@"d",@"e", nil]; -
NSSet *set10 = [NSSet setWithObjects:@"x",@"y",@"z", nil]; -
NSSet *set11 = [NSSet setWithObjects:@"x",@"y", nil]; -
BOOL isEqual = [set8 isEqualToSet:set9];//判断两个集合是否匹配 -
if (isEqual == 1) { -
NSLog(@"equal"); -
} -
else{ -
NSLog(@"not equal"); -
} -
BOOL isInset = [set8 intersectsSet:set10];//判断两个集合是否存在交集 -
if (isInset == 1) { -
NSLog(@"yes"); -
} -
else -
{ -
NSLog(@"no"); -
} -
BOOL isSub = [set11 isSubsetOfSet:set10];//判断两个集合是否是另一个的字迹 -
if (isSub == 1) { -
NSLog(@"set11 是 set10 的子集"); -
} -
else -
{ -
NSLog(@"set11 不是 set10 的子集"); -
} -
-
NSLog(@"************************************************************"); -
NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"1",@"2",@"3",@"4", nil]; - //
NSLog(@"%@",mutableSet1); -
NSMutableSet *mutableSet2 = [NSMutableSet setWithObjects:@"3",@"4",@"5", nil]; -
- //
[mutableSet2 minusSet:mutableSet1];//两个集合相减,即当前者和后者有交集的时候,减去前者的交集,后者不变 - //
NSLog(@"两个集合相减%@",mutableSet2); -
-
[mutableSet1 unionSet:mutableSet2];//两个集合相加,即当前者和后者有交集的时候,前者加上后者多出的元素,后者不变 -
NSLog(@"两个集合相加:%@",mutableSet1); -
-
-
[mutableSet1 intersectSet:set8];//获取不可变的集合与可变集合的交集 -
NSLog(@"获得两个集合的交集%@",mutableSet1); -
-
} -
return 0; - }
OC学习之NSDictionary,NSSet
最新推荐文章于 2020-12-29 06:27:14 发布