天天看点

字典、集合的用法

        // 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",

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

        nsarray *valuearray =

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",

        nsenumerator *e =

[arr objectenumerator];

        nsstring *str5 =

        while (str5 = [e

nslog(@"%@",str5);

nsmutabledictionary *dic1 =

[nsmutabledictionarydictionarywithobject:@"value1"forkey:@"k1"];

nsmutabledictionary *dic2

=[nsmutabledictionarydictionarywithobject:@"value2"forkey:@"k2"];

        [dic1

addentriesfromdictionary:dic2];

nslog(@"%@",dic1);

removeallobjects];

setvalue:@"lalala"

forkey:@"123"];

        //nsset

//1.使用类方法创建

        nsset *set1 =

[nsset set];

//创建一个空的集合对象

        nsset *set2 =

[nssetsetwithobject:@"abc"];

        nsset *set3 =

[nsset

setwithobjects:@"hahaha",@"hehehe",@"eee",

nslog(@"%@", set2);

nslog(@"%@", set3);

[nsarrayarraywithobjects:@"abc",@"bcd",@"cdf",

        nsset *set4 =

[nsset setwitharray:arr];

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

        nsset *set5 =

[nssetsetwithobject:@"wow"];

nslog(@"%@", set5);

        nsset *set6 =

[nsset setwithset:set3];

        nsset *set7 =

[nsset setwithobjects:set6,set2,

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

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

        nsset *s1 =

[[nssetalloc]initwithobjects:@"abc",@"bcd",

        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]);

[s3 objectenumerator];

        nsstring *str

=nil;

        while (str = [e

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

        nsenumerator *e1 =

        while (str5 = [e1

nslog(@"%@", str5);

//判断是否有交集

nslog(@"%d",[set4

intersectsset:s1]);

//判断是否相等

isequaltoset:s3]);

//判断是否是子集

issubsetofset:set5]);

        nsmutableset *s6 =

[nsmutablesetsetwithobjects:@"hhh",@"jjj",@"kkk",@"nnn",nil];

        nsmutableset *s7 =

[nsmutablesetsetwithobjects:@"hhh",@"jjj",

        [s6

intersectset:s7]; //intersectsset

多个intersect“s“是判断是否有交集

nslog(@"%@",s6);

addobject:@"aaa"];

        [s6 minusset:s7];

//取2个集合的并集

        [s6 unionset:s7];

        //给集合赋值

        [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;

return nsorderedsame;