zoukankan      html  css  js  c++  java
  • 字典、集合的用法

            // NSDictionary

            NSDictionary *dictionary = [[ NSDictionaryalloc]initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2"@"v3", @"k3", @"vc9", @"ko0",@"M",@"Ang",@"9",@"aa", @"aa", @"2", @"sg", @"-2", nil];

            NSLog(@"%@", dictionary);

            

            //使用类方法创建

            NSDictionary *d = [NSDictionarydictionaryWithObjectsAndKeys:@"abc", @"kkk1", @"ABC", @"k333", nil];

            NSLog(@"%@", d);

            //只能赋一个值的方法

            NSDictionary *d1 = [NSDictionarydictionaryWithObject:@"vv3"forKey:@"kkkkkkk5"];

            NSLog(@"%@", d1);

            

            

            //创建保存索引key的数组

            NSArray *keyArray = [NSArray arrayWithObjects:@"K1",@"K3",@"K5", nil];

            //创建保存所有value的数组

            NSArray *valueArray = [NSArray arrayWithObjects:@"v1", @"v3",@"v5", nil];

            NSDictionary *d2 = [NSDictionarydictionaryWithObjects:valueArray forKeys:keyArray];

            NSLog(@"%@", d2);

            

            //使用一个文件创建字典对象

            NSDictionary *d5 = [NSDictionarydictionaryWithContentsOfFile:@"/Users/lanou3g/Desktop/Deme_4.25/Deme_4.25/Dict.List.plist"];

            NSLog(@"%ld",[d5 count]);

            

            

            //根据Key取出一个值

            NSString *p1 = [d5 objectForKey:@"abcd"];

            NSLog(@"%@", p1);

            

            //取出字典中所有的key

            NSArray *allkeys = [d5 allKeys];

            NSLog(@"%@", allkeys);

            

            //取出所有value

            NSArray *allvalue = [d5 allValues];

            NSLog(@"%@", allvalue);

            

            //使用枚举器

            NSEnumerator *enumerator =[d5 keyEnumerator];

            NSString *str = nil;

            while (str = [enumerator nextObject]) {

                NSLog(@"%@", str);

            }

            

            //数组中使用枚举器

            NSArray *arr = [NSArrayarrayWithObjects:@"hjds",@"jhnj",@"ihi", nil];

            NSEnumerator *e = [arr objectEnumerator];

            NSString *str5 = nil;

            while (str5 = [e nextObject]) {

                NSLog(@"%@",str5);

            }

            

            NSMutableDictionary *dic1 = [NSMutableDictionarydictionaryWithObject:@"value1"forKey:@"k1"];

            NSMutableDictionary *dic2 =[NSMutableDictionarydictionaryWithObject:@"value2"forKey:@"k2"];

            

            [dic1 addEntriesFromDictionary:dic2];

            NSLog(@"%@",dic1);

            [dic1 removeAllObjects];

            NSLog(@"%@",dic1);

            [dic1 setValue:@"lalala" forKey:@"123"];

            NSLog(@"%@",dic1);

            //NSSet

            //1.使用类方法创建

            NSSet *set1 = [NSSet set]; //创建一个空的集合对象

            NSSet *set2 = [NSSetsetWithObject:@"abc"];

            NSSet *set3 = [NSSet setWithObjects:@"hahaha",@"hehehe",@"eee", nil];

            NSLog(@"%@", set2);

            NSLog(@"%@", set3);

            NSArray *arr = [NSArrayarrayWithObjects:@"abc",@"bcd",@"cdf", nil];

            NSSet *set4 = [NSSet setWithArray:arr];

            NSLog(@"----------%@", set4);

            

            NSSet *set5 = [NSSetsetWithObject:@"wow"];

            NSLog(@"%@", set5);

            

            NSSet *set6 = [NSSet setWithSet:set3];

            NSSet *set7 = [NSSet setWithObjects:set6,set2, nil];

            NSLog(@"+++++%@", set7);

            

            //使用实例方法创建集合

            NSSet *s1 = [[NSSetalloc]initWithObjects:@"abc",@"bcd", nil];

            NSSet *s2 = [[NSSetalloc]initWithSet:s1];

            NSLog(@"%@", s2);

            NSSet *s3 = [[NSSetalloc]initWithArray:[NSArrayarrayWithObjects:@"abc",@"bcd",@"cdf", nil]];

            NSLog(@"%@", s3);

            NSSet *s4 = [[NSSetalloc]initWithSet:s1 copyItems:NO];

            NSLog(@"%@", s4);

            NSLog(@"%ld",[s3 count]);

            

            //使用枚举器

            NSEnumerator *e = [s3 objectEnumerator];

            NSString *str =Nil;

            while (str = [e nextObject]) {

                NSLog(@"使用枚举器:%@",str);

            }

            NSEnumerator *e1 = [s3 objectEnumerator];

            NSString *str5 = nil;

            while (str5 = [e1 nextObject]) {

                NSLog(@"%@", str5);

            }

            

            //判断是否有交集

            NSLog(@"%d",[set4 intersectsSet:s1]);

            //判断是否相等

            NSLog(@"%d",[set4 isEqualToSet:s3]);

            //判断是否是子集

            NSLog(@"%d",[set4 isSubsetOfSet:set5]);

            

            

            NSMutableSet *s6 = [NSMutableSetsetWithObjects:@"hhh",@"jjj",@"kkk",@"nnn",nil];

            NSMutableSet *s7 = [NSMutableSetsetWithObjects:@"hhh",@"jjj", nil];

            [s6 intersectSet:s7]; //intersectsSet 多个intersect“s“是判断是否有交集

            NSLog(@"%@",s6);

            [s6 addObject:@"aaa"];

            NSLog(@"%@",s6);

            [s6 minusSet:s7];

            NSLog(@"%@",s6);

            

            //2个集合的并集

            [s6 unionSet:s7];

            NSLog(@"%@",s6);

            //给集合赋值

            [s7 setSet: s3];

            NSLog(@"%@",s7);

        }

        return 0;

    }

            NSInteger intSort(id num1, id num2, void *context)

            {

                int v1 = [num1 intValue];

                int v2 = [num2 intValue];

                

                if (v1 < v2)

                    return NSOrderedAscending;

                else if (v1 > v2)

                    returnNSOrderedDescending;

                else

                    return NSOrderedSame;

            }

  • 相关阅读:
    Python 函数装饰器简明教程
    *arg和**kwarg的区别
    克里金插值
    C语言Hello world
    ibatis错误
    typealias
    视图
    权限分级设置
    走出浮躁的泥沼:学会享受学习过程的乐趣
    R语言 eval(quote(x)) 和 eval(x)
  • 原文地址:https://www.cnblogs.com/mingtiannihao/p/3721034.html
Copyright © 2011-2022 走看看