集合:NSArray、NSMutableArray、NSDictionary、NSMutableDictionary、NSSet、NSMutableSet使用

来源:互联网 发布:手机短信同步软件 编辑:程序博客网 时间:2024/05/18 00:58

    /*本文介绍了:NSArray(有序的集合,存储的元素在一个整块的内存中并按序排列)、     NSMutableArray、     NSDictionary(键值映射)、     NSMutableDictionary、     NSSet(无序的集合,散列存储)、     NSMutableSet, 类方法参见官方API*/
        //一、数组 NSArray    // 1‘创建数组    NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];        //2、-(unsigned) count数组所包含对象个数    NSLog(@"self.dataArray cound:%lu", [array count]);        //3、- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象;    NSLog(@"self.dataArray cound 2:%@",[array objectAtIndex:2]);        //切分数组    //4、从字符串分割到数组- componentsSeparatedByString:    NSString *string = [[NSString alloc] initWithString:@"One,Two,Three,Four"];    NSLog(@"string:%@",string);    NSArray *array = [string componentsSeparatedByString:@","];    NSLog(@"array:%@",array);    [string release];        //5、从数组合并元素到字符串- componentsJoinedByString:    NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];    NSString *string = [array componentsJoinedByString:@","];    NSLog(@"string:%@",string);        //二、从一个数组拷贝数据到另一数组(可变数级)NSMutableArray    //1、arrayWithArray:    NSArray *array2 = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];    NSLog(@"array:%@",array2);        NSMutableArray *mutableArray = [[NSMutableArray alloc] init];    mutableArray = [NSMutableArray arrayWithArray:array2];    NSLog(@"MutableArray:%@",mutableArray);        NSArray *array1 = [[NSArray alloc] init];    array1 = [NSArray arrayWithArray:array2];    NSLog(@"array1:%@",array1);        //2、Copy    NSMutableArray *newArray = [[NSMutableArray alloc] init];    NSArray *oldArray = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];    NSLog(@"oldArray:%@",oldArray);        id obj;    for(int i = 0; i < [oldArray count]; i++)    {        obj = [[oldArray objectAtIndex:i] copy];        [newArray addObject: obj];    }    NSLog(@"newArray:%@", newArray);    [newArray release];        //3、快速枚举    NSMutableArray *newArray = [[NSMutableArray alloc] init];    NSArray *oldArray2 = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];    NSLog(@"oldArray:%@", oldArray2);        for(id obj in oldArray2)    {        [newArray addObject: obj];    }    NSLog(@"newArray:%@", newArray);    [newArray release];        //4、Deep copy    NSArray *oldArray = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];    NSLog(@"oldArray:%@",oldArray);        NSMutableArray *newArray = [[NSMutableArray alloc] init];    newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault,  (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);    NSLog(@"newArray:%@", newArray);    [newArray release];        //5、Copy and sort    NSArray *oldArray = [NSArray arrayWithObjects:@"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];    NSLog(@"oldArray:%@",oldArray);        NSMutableArray *newArray = [[NSMutableArray alloc] init];    NSEnumerator *enumerator = [oldArray objectEnumerator];    id obj;    while(obj = [enumerator nextObject])    {        [newArray addObject: obj];    }    [newArray sortUsingSelector:@selector(compare:)];    NSLog(@"newArray:%@", newArray);    [newArray release];        //6、给数组分配容量    NSArray *array = [NSMutableArray arrayWithCapacity:20];        //7、在数组末尾添加对象,- (void) addObject: (id) anObject;    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];    [array addObject:@"Four"];    NSLog(@"array:%@",array);        //8、删除数组中指定索引处对象,-(void) removeObjectAtIndex: (unsigned) index;    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];    [array removeObjectAtIndex:1];    NSLog(@"array:%@",array);        //9、数组枚举,- (NSEnumerator *)objectEnumerator;从前向后    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];    NSEnumerator *enumerator = [array objectEnumerator];    id thingie;    while (thingie = [enumerator nextObject]) {        NSLog(@"thingie:%@",thingie);    }        //10、从后向前,-(NSEnumerator *)reverseObjectEnumerator;    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];    NSEnumerator *enumerator = [array reverseObjectEnumerator];    id object;    while (object = [enumerator nextObject]) {        NSLog(@"object:%@",object);    }        //11、快速枚举    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];    for(NSString *string in array)    {        NSLog(@"string:%@",string);    }        //三、字典 NSDictionary,key/value 方式,对应于Java中Map    //1、创建字典,- (id) initWithObjectsAndKeys;    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];    NSString *string = [dictionary objectForKey:@"One"];    NSLog(@"string:%@",string);    NSLog(@"dictionary:%@",dictionary);    [dictionary release];        //四、可变字典 NSMutableDictionary    //1、创建可变字典    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];    [dictionary setObject:@"One" forKey:@"1"];    [dictionary setObject:@"Two" forKey:@"2"];    [dictionary setObject:@"Three" forKey:@"3"];    [dictionary setObject:@"Four" forKey:@"4"];    NSLog(@"dictionary:%@",dictionary);        //2、删除指定的字典    [dictionary removeObjectForKey:@"3"];    NSLog(@"dictionary:%@",dictionary);        //3、对任何对象进行包装NSValue, 将NSRect放入NSArray中,将NSRect放入NSArray中    NSMutableArray *array = [[NSMutableArray alloc] init];    CGRect rect = CGRectMake(0, 0, 320, 480);    NSValue *value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];    [array addObject:value];    NSLog(@"array:%@",array);        //4、从Array中提取    value = [array objectAtIndex:0];    [value getValue:&rect];    NSLog(@"value:%@",value);        //5、从目录搜索扩展名为jpg的文件    NSString *home = @"../Users/";    NSFileManager *fileManager = [NSFileManager defaultManager];    NSDirectoryEnumerator *direnum = [fileManager enumeratorAtPath: home];    NSMutableArray *files = [[NSMutableArray alloc] init];        NSString *filename;    while (filename = [direnum nextObject]) {        if([[filename pathExtension] hasSuffix:@"jpg"]){            [files addObject:filename];        }    }        //快速枚举    for(NSString *filename in direnum){        if([[filename pathExtension] isEqualToString:@"jpg"]){            [files addObject:filename];        }    }    NSLog(@"files:%@",files);        //6、枚举    NSEnumerator *filenum = [files objectEnumerator];    while (filename = [filenum nextObject]) {        NSLog(@"filename:%@",filename);    }        //快速枚举    for(id object in files){        NSLog(@"object:%@",object);    }        //五、NSSet    //1、NSSet的使用    /*     [NSSet setWithSet:(NSSet *)set]; 用另外一个set对象构造     [NSSet setWithArray:(NSArray *)array];用数组构造     [NSSet setWithObjects:...]:创建集合对象,并且初始化集合中的数值,结尾必需使用nil标志。     [set count] ; 得到这个结合对象的长度。     [set containsObject:...]: 判断这个集合中是否存在传入的对象,返回Bool值。     [set objectEnumerator]: 将集合放入迭代器。     [enumerator nextObject]:得到迭代器中的下一个节点数据,使用while遍历这个迭代器,方可遍历集合对象中的对象。     [set isEqualToSet:objset]:判断两个集合是否完全相等,返回Bool值。     [set isSubsetOfSet:objset]:判断集合中的所有数据是否都相等与objeset集合中,返回Bool值。     [set allObjects];     */    NSSet *set = [NSSet setWithObjects:@"25",@"age",@"张三",@"name",@"男",nil];    NSSet *set1 = [NSSet setWithObjects:@"25",@"age",@"张三",@"name",@"男",@"性别",nil];    NSLog(@"set count:%lu", [set count]);        //判断是否含有age字符串    if([set containsObject:@"age"]) {        NSLog(@"set包含age");    }        //判断set 是否等于set1    if ([set isEqualToSet:set1]) {        NSLog(@"set 等于 set1");    }        //判断set是否是否是set1的子集合    if ([set isSubsetOfSet:set1]) {        NSLog(@"set isSubsetOfSet set1");    }        //获取所有set对象    NSArray *array = [set allObjects];    NSLog(@"array:%@", array);        //迭代遍历    NSEnumerator *enumerator = [set objectEnumerator];    for (NSObject *object in enumerator) {        NSLog(@"set1里的对象:%@", object);    }        //六、NSMutableSet的使用    /*     NSMutableSet继承NSSet,它可以使用NSSet的方法。     [NSMutableSet setWithCapacity:6]:创建可变集合对象,并且初始化长度为6。     [set addObject: obj] : 向集合中动态的添加对象。     [set removeObject:obj]:删除集合中的一个对象。     [set removeAllObjects]:删除集合中的所有对象。     [set unionSet:obj]:向集合中添加一个obj集合的所有数据。     [set minusSet:obj]:向集合中删除一个obj集合的所有数据。     [set intersectSet]:向集合中删除一个不包含obj集合的所有数据。     */    //集合NSMutableSet中不能存在重复的对象    NSMutableSet *set1 = [[NSMutableSet alloc] initWithObjects:@"1",@"2",@"3", nil];    NSMutableSet *set2 = [[NSMutableSet alloc] initWithObjects:@"1",@"5",@"6", nil];    [set1 unionSet:set2];      //取并集1,2,3,5,6    [set1 intersectSet:set2];  //取交集1    [set1 minusSet:set2];      //取差集2,3,5,6


原创粉丝点击