集合

来源:互联网 发布:linux root鉴定错误 编辑:程序博客网 时间:2024/06/03 03:02

集合:用来存储对象的容器

集合中不可变的类都是可变的类的父类。子类功能更强大,有父类的功能和自身的功能。集合都是以nil结束。

集合中只有数组有序。字典与set都是散列存储(hash),查找快。

arrayWithContentsOfFIle:arrayWithContentsOfURL:

如果文件存的是字符串,读取出来的也是字符串。

如果文件存的是数组,读取出来的也是数组。

如果文件存的是字典,读取出来的也是字典。

如果文件存的是集合,读取出来的也是集合。

不可变都是不支持对对象增删改操作。


 

一.数组(可存重复对象)

1.不可变

  • NSArray(不可对对象增删改)可存放多个对象,每个对象使用下标来表示。
  • CFArrayRef(C语言用的数组,用于操作硬件)
  • for-in:(in 相当于赋值NSString *s = array)快速遍历同一类型对象
  • 线程安全

2.可变

集合的内存管理中,如果一个对象(或数组)被加到集合中,集合会使引用计数加1,控制权归集合(系统)。

二.字典

  1. 以键值对、无序的存储。
  2. key决定集合个数,key重复会覆盖,一般字符串类型,实际是id。
  3. 字典有区分范围的作用。当value为数组时(广州),当value为字典时(广州-天河)
  4. %@可以输出对象描述,起到很好的调试作用。
  5. 字典的操作都是通过key来实现,对key排序就可以让整个字典有序(但通常很少这个需求)。

三.set

无序集合,存储的对象最好不要重复,重复会覆盖。

四.转换


 

1数组

 //oc可变参数    NSArray *arr1 = [[NSArray alloc]initWithObjects:@"jobs",@"bill",@"keati",nil];
  BOOL b = [arr1 containsObject:@"jobs"];
  NSLog(@"b = %d",b);
//访问对象 NSString *str0 = [arr1 objectAtIndex:0]; NSLog(@"str0 = %@",str0); NSLog(@"%@",[arr1 lastObject]);//可以实现栈操作 NSLog(@"%d",arr1.count); //返回元素位置 NSLog(@"jobs an index:%d",[arr1 indexOfObject:@"jobs"]); for (int i = 0; i < arr1.count;i++) { //NSLog(@"%@",[arr1 objectAtIndex:i]); NSString *str = [arr1 objectAtIndex:i]; NSString *newStr = [str stringByAppendingString:@"1"]; NSLog(@"%@",newStr); } //for-in 快速遍历(指针指向arr1) for (NSString *str in arr1) { NSString *newStr = [str stringByAppendingString:@"2"]; NSLog(@"str = %@",newStr); } NSLog(@" ");
  //使用NSEnumerator -(id)nextObject;
   NSEnumerator *enumer = [arr1 objectEnumerator];
   id obj = nil;
   while(obj = [enumer nextObject])
   {NSLog(@"%@",obj)}
//排序 NSArray *orderArray = [arr1 sortedArrayUsingSelector:@selector(compare:)]; for (NSString *s in orderArray) { NSLog(@"%@",s); } //新语法(数组转化成字符串)[arr1 objectAtIndex:0] NSString *str1 = arr1[0]; NSLog(@"str1 = %@",str1); //可变数组 NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:3]; Student *stu1 = [[Student alloc]init]; stu1.name = @"batman"; NSLog(@"stu1 retainCount:%d",[stu1 retainCount]); //添加元素 [mArr addObject:stu1];//系统retian的 NSLog(@"stu1 retainCount:%d",[stu1 retainCount]); Student *stu2 = [[Student alloc]init]; stu2.name = @"oxman"; [mArr addObject:stu2]; NSLog(@"stu1 retainCount:%d",[stu2 retainCount]); Student *stu3 = [[Student alloc]init]; stu3.name = @"superman"; [mArr addObject:stu3]; NSLog(@"stu1 retainCount:%d",[stu3 retainCount]); NSLog(@"mArrCount1 = %@",mArr); [mArr insertObject:stu3 atIndex:0]; //s3,s1,s2,s3 NSLog(@"mArrCount2 = %@",mArr); [mArr removeObjectIdenticalTo:stu3];//remove同一对象,同时引用计数减一 NSLog(@"%@",mArr); [mArr replaceObjectAtIndex:0 withObject:stu1]; NSLog(@"%@",mArr); // NSArray *arr = @[@"3",@"2",@"1"];//新语法 NSMutableArray *mArray = [NSMutableArray arrayWithArray:arr];//父类转子类需通过这层转 [self bubbles:mArray]; NSLog(@"%@",mArray);
-(void)bubbles:(NSMutableArray *)aMutableArray{    int count = aMutableArray.count;    for (int i = 0; i < count - 1; i++)    {        for (int j = 0; j < count -1 - i; j++)        {            int a = [[aMutableArray objectAtIndex:j]integerValue];            int b = [[aMutableArray objectAtIndex:j+1]integerValue];            if (a > b)            {                [aMutableArray exchangeObjectAtIndex:j withObjectAtIndex:j+1];            }        }    }}

2.字典

NSDictionary *dic1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"value1",@"key1",                          @"value3",@"key3",                          @"value2",@"key2",nil];        //快速输出    NSLog(@"%@",dic1);    //访问    NSString *str = [dic1 valueForKey:@"key1"];    NSLog(@"str = %@",str);    //不能添加,修改,删除    //元素个数    NSLog(@"dic1 count:%d",dic1.count);    //获取所有keys    NSArray *keys = [dic1 allKeys];    //获取所有values    NSArray *values = [dic1 allValues];    NSLog(@"keys = %@",keys);    NSLog(@"values = %@",values);    //遍历    for(NSString *key in dic1.allKeys)    {        NSString *v = [dic1 valueForKey:key];        NSLog(@"dic = %@",v);    }    //对key排序    NSLog(@"排序后的字典");    NSArray *sortKeys = [keys sortedArrayUsingComparator:^(id obj1,id obj2)    {        return [obj1 compare:obj2];    }];    //遍历    for(NSString *key in sortKeys)    {        NSString *v = [dic1 valueForKey:key];        NSLog(@"dic = %@",v);    }        //可变字典    NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithCapacity:5];    //添加元素    [dic2 setObject:@"value1" forKey:@"key1"];        Student *s = [[Student alloc]init];    s.name = @"jobs";        //添加value为student类型对象的元素    [dic2 setObject:s forKey:@"jobs"];    NSLog(@"dic2 = %@",dic2);        //通过覆盖key值来修改    [dic2 setObject:@"jobs" forKey:@"jobs"];    NSLog(@"dic2 = %@",dic2);        //通过删除key值删元素    [dic2 removeObjectForKey:@"jobs"];    NSLog(@"dic2 = %@",dic2);

3.

Set:

NSSet *set1 = [[NSSet alloc]initWithObjects:@"1",@"2",@"3",@"1",nil];    NSSet *set2 =[NSSet setWithObjects:@"2", nil];        BOOL isSubSet = [set2 isSubsetOfSet:set1];//set2是否set1的子集    //快速输出    NSLog(@"set = %@",set1);    //求交集    BOOL isInter = [set2 intersectsSet:set1];

NSMutableSet:

NSSet+Printing.h

#import <Foundation/Foundation.h>@interface NSSet (Printing)-(void)print;@end

NSSet+Printing.m

#import "NSSet+Printing.h"@implementation NSSet (Printing)-(void)print{    printf("{ ");        for (NSString *element in self)    {        printf("%d\t",[element integerValue]);    }        printf(" }\n");}@end

AppDelegate.m

#import "AppDelegate.h"#import "NSSet+Printing.h"#define INTOBJ(v) [NSString stringWithFormat:@"%d",v]@implementation AppDelegate
NSMutableSet *set1 = [NSMutableSet setWithObjects:INTOBJ(1),INTOBJ(3),INTOBJ(5),INTOBJ(10),INTOBJ(-5), nil];        NSMutableSet *set2 = [NSMutableSet setWithObjects:INTOBJ(-5),INTOBJ(100),INTOBJ(3),INTOBJ(5),nil];        NSMutableSet *set3 = [NSMutableSet setWithObjects:INTOBJ(12),INTOBJ(200),INTOBJ(3),nil];        NSLog(@"set1 :");    [set1 print];    NSLog(@"set2 :");    [set2 print];    //相等性测试    if ([set1 isEqualToSet:set2])    {        NSLog(@"set1 equal set2");    }    else    {        NSLog(@"set1 is not equal set2");    }    //成员测试    if ([set1 containsObject:INTOBJ(10)] == YES)    {        NSLog(@"set1 contains 10");    }    else    {        NSLog(@"set1 does not contains 10");    }    if ([set2 containsObject:INTOBJ(10)] == YES)    {        NSLog(@"set2 contains 10");    }    else    {        NSLog(@"set2 does not contains 10");    }    //在可变集合set1中添加和移除对象    [set1 addObject:INTOBJ(4)];    [set1 removeObject:INTOBJ(10)];    NSLog(@"set after adding 4 and removing 10:");    [set1 print];        //获得两集合交集    [set1 intersectSet:set2];    printf("set1的交集:");    [set1 print];    //获得两集合并集    [set1 unionSet:set3];    printf("set1的并集:");    [set1 print];

4.对象类型与结构类型的转换

//对基本数据类型转为对象类型    NSLog(@"%@",[NSNumber numberWithInt:5]);    NSNumber *number = [[NSNumber alloc]initWithInt:9];    NSLog(@"%@",number);    //对象类型转整形    NSLog(@"%d",[number integerValue]);    //把结构体转为对象类型    CGPoint p = {100,100};    NSValue *val = [NSValue valueWithBytes:&p objCType:@encode(CGPoint)];    NSLog(@"%@",val);    //对象类型转为结构体    CGPoint q;    [val getValue:&q];    NSLog(@"%f,%f",q.x,q.y);    //得到一个空对象,可以存到集合,NULL是BOOL类型赋空值.nil是对象赋空值,null是方法,Nil是类为空    NSLog(@"%@",[NSNull null]);

 CGSize:长宽

 CGRect:矩形

 NSRange:范围

 

0 0
原创粉丝点击