OC中的集合类-NSSet、NSArray、NSDictionary

OC中的集合类-NSSet、NSArray、NSDictionary,第1张

概述三种集合类来收集cocoa对象(NSObject对象): NSArray 用于对象有序集合(相当于是数组) NSSet 用于对象无序集合 NSDictionary用于键值映射 以上三种集合类是不可变的(一旦初始化后,就不能改变) 以下是对应的三种可变集合类(这三种可变集合类是对应上面三种集合类的子类): NSMutableArray NSMutableSet NSMutableDictionary 三种集合类来收集cocoa对象(NSObject对象):
NSArray 用于对象有序集合(相当于是数组)
NSSet 用于对象无序集合
NSDictionary用于键值映射
以上三种集合类是不可变的(一旦初始化后,就不能改变)

以下是对应的三种可变集合类(这三种可变集合类是对应上面三种集合类的子类):
NSMutableArray
NSMutableSet
NSMutableDictionary

注:这些集合类只能收集cocoa对象(NSOjbect对象),如果想保存一些原始的C数据(例如,int,float,double,BOol等),则需要将这些原始的C数据封装成NSNumber类型的,NSNumber对象是cocoa对象,可以被保存在集合类中。


NSArray

-----创建数组 -----
    NSArray *array = [[NSArray alloc] initWithObjects:
    @"One",@"Two",@"Three",@"Four",nil];

    self.dataArray = array;
    [array release];

    - (unsigned) Count;数组所包含对象个数;

    - (ID) objectAtIndex: (unsigned int) index;获取指定索引处的对象;

------ 从一个数组拷贝数据到另一数组(可变数级) -------   

    //arrayWithArray:
    NSArray *array1 = [[NSArray alloc] init];
    NSMutableArray *MutableArray = [[NSMutableArray alloc] init];
    NSArray *array = [NSArray arrayWithObjects:
                      @"a",@"b",@"c",nil];

    MutableArray = [NSMutableArray arrayWithArray:array];

    array1 = [NSArray arrayWithArray:array];

    //copy

    //ID obj;
    NSMutableArray *newArray = [[NSMutableArray alloc] init];
    NSArray *oldArray = [NSArray arrayWithObjects:
                         @"a",@"d",@"e",@"f",@"g",@"h",nil];
    for(int i = 0; i < [oldArray count]; i++)
    {        
        obj = [[oldArray objectAtIndex:i] copy];
        [newArray addobject: obj];
    }
    [newArray release];

//快速枚举
   
NSMutableArray *newArray = [[NSMutableArray alloc] init];
    NSArray *oldArray = [NSArray arrayWithObjects:
                         @"a",nil];  

    for(ID obj in oldArray)
    {
        [newArray addobject: obj];
    }
    [newArray release];   

    //Deep copy

    NSMutableArray *newArray = [[NSMutableArray alloc] init];
    NSArray *oldArray = [NSArray arrayWithObjects:
                         @"a",nil];  
    newArray = (NSMutableArray*)CFPropertyListCreateDeepcopy(kcfAllocatorDefault,(CFPropertyListRef)oldArray,kcfPropertyListMutableContainers);
    [newArray release];  

NSMutableArray

- (voID)addobject:(ID)anObject;
- (voID)insertObject:(ID)anObject atIndex:(int)index;
- (voID)removeObjectAtIndex:(int)index;
- (voID)removeAllObjects;

-----给数组分配容量-----
    array = [NSMutableArray arrayWithCapacity:20];

-----在数组末尾添加对象-----
    //- (voID) addobject: (ID) anObject;
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",nil];
    [array addobject:@"Four"];

-----删除数组中指定索引处对象-----   
    //-(voID) removeObjectAtIndex: (unsigned) index;   
    //NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",nil];
    [array removeObjectAtIndex:1];

 ----- 数组枚举-----  
//1、- (NSEnumerator *)objectEnumerator;  //从前向后
    //NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",nil];
    NSEnumerator *enumerator;
    enumerator = [array objectEnumerator];

    ID thingIE;
    while (thingIE = [enumerator nextObject]) {
        NSLog(@"thingIE:%@",thingIE);
    }
//2、- (NSEnumerator *)reverSEObjectEnumerator;  //从后向前
    //NSMutableArray *array = [NSMutableArray arrayWithObjects:
    @"One",nil];
    NSEnumerator *enumerator;
    enumerator = [array reverSEObjectEnumerator];

    ID object;
    while (object = [enumerator nextObject]) {
        NSLog(@"object:%@",object);
    }
//3、快速枚举
    //NSMutableArray *array = [NSMutableArray arrayWithObjects:
    @"One",nil];
    for(Nsstring *string in array)
    {
        NSLog(@"string:%@",string);
    }

----- NSValue(对任何对象进行包装)-----
//将NSRect放入NSArray中
    NSMutableArray *array = [[NSMutableArray alloc] init];
    NSValue *value;
    CGRect rect = CGRectMake(0,320,480);   
    value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];
    [array addobject:value];
//从Array中 提取
    value = [array objectAtIndex:0];
    [value getValue:&rect];

----★使用NSMutableArray要防止内存泄露★------
NSObject* p1 = [[NSObject alloc] init];
NSObject* p2 = [[NSObject alloc] init];
NSMutableArray* objectsArray = [[NSMutableArray alloc] init];

[objectsArray addobject:p1];
NSLog(@"p1 count:%d",[p1 retainCount]);//输出 2,也就是执行追加对象后,对象的计数器也被加1
//[p1 release];
//NSLog(@"p1 count:%d",[p1 retainCount]);

//同样做数组替换时
[objectsArray replaceObjectAtIndex:0 withObject:p2];
NSLog(@"p2 count:%d",[p2 retainCount]);//输出 2,同样也是2
NSLog(@"p1 count:%d",[p1 retainCount]);//输出 1,对象p1仍然存在
//[p2 release];
//NSLog(@"p2 count:%d",[p2 retainCount]);

//执行清空数组
[objectsArray removeAllObjects];
NSLog(@"p2 count:%d",[p2 retainCount]);//输出 1,对象p2仍然存在
//[p2 release];

由此可知,每次执行上面的数组 *** 作后,要执行对象release,如上面注释中的语句,才能保证内存不泄露。


NSSet
Unordered collection of objects.
Immutable. You cannot add or remove objects to it once it’s created.
important methods:
+ set
WithObjects:(ID)firstObj,...;  // nil terminated

- (int)count;
- (BOol)containsObject:(ID)anObject;
- (ID)anyObject;
- (voID)makeObjectsPerformSelector:(SEL)aSelector;
- (ID)member:(ID)anObject; // uses isEqual: and returns a matching object (if any)

NSMutableSet
Mutable version of NSSet.
+ (NSMutableSet *)set;
- (voID)addobject:(ID)anObject;
- (voID)removeObject:(ID)anObject;
- (voID)removeAllObjects;
- (voID)unionSet:(NSSet *)otherSet;
- (voID)minusSet:(NSSet *)otherSet;
- (voID)intersectSet:(NSSet *)otherSet;

NSDictionary
key-value,key-value,..... 一系列键值对。
key(键)在整个dictionary是唯一的,通过key可以查询其对应的一个或多个value(值)。 Hash table. Look up objects using a key to get a value.
Immutable. You cannot add or remove objects to it once it’s created.
Keys are objects which must implement.  Keys are usually Nsstring objects.
- (NSUInteger)hash & - (BOol)isEqual:(NSObject *)obj
important methods:
+ dictionaryWithObjectsAndKeys: (ID)firstObject,...;
- (int)count;
- (ID)objectForKey:(ID)key;
- (NSArray *)allKeys;
- (NSArray *)allValues;
 -----创建字典 -----
    - (ID) initWithObjectsAndKeys;

     NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"2",@"3",nil];
    Nsstring *string = [dictionary objectForKey:@"One"];
    NSLog(@"string:%@",string);
    NSLog(@"dictionary:%@",dictionary);
    [dictionary release];

NSMutableDictionary
+ (NSMutableDictionary *)dictionary;
- (voID)setobject:(ID)anObject forKey:(ID)key;
- (voID)removeObjectForKey:(ID)key;
- (voID)removeAllObjects;
- (voID)addEntrIEsFromDictionary:(NSDictionary *)otherDictionary;
 -----创建可变字典 -----   
//创建
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
添加字典
    [dictionary setobject:@"One" forKey:@"1"];
    [dictionary setobject:@"Two" forKey:@"2"];
    [dictionary setobject:@"Three" forKey:@"3"];
    [dictionary setobject:@"Four" forKey:@"4"];
//删除指定的字典     [dictionary removeObjectForKey:@"3"];
总结

以上是内存溢出为你收集整理的OC中的集合类-NSSet、NSArray、NSDictionary全部内容,希望文章能够帮你解决OC中的集合类-NSSet、NSArray、NSDictionary所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1057189.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存