NSArray,NSSet,NSDictionary总结

博客分类:

  • iphone

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

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

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

NSArray
Ordered collection of objects. Immutable. You cannot add or remove objects to it once it’s created.
Important methods:
+ (id)arrayWithObjects:(id)firstObject, ...;     // nil terminated 
- (int)count;
- (id)objectAtIndex:(int)index;                     //  NSString *s1=[[myarray objectAtIndex: 0];
- (void)makeObjectsPerformSelector:(SEL)aSelector;
- (NSArray *)sortedArrayUsingSelector:(SEL)aSelector;
- (id)lastObject; // returns nil if there are no objects in the array (convenient)
注:
类方法arrayWithObjects 可以创建an autoreleased NSArray of the items.例如
@implementation MyObject
- (NSArray *)coolCats {
return [NSArray arrayWithObjects:@“Steve”, @“Ankush”, @“Sean”, nil];
}
@end
Other convenient create with methods (all return autoreleased objects):
[NSString stringWithFormat:@“Meaning of %@ is %d”, @“life”, 42];
[NSDictionary dictionaryWithObjectsAndKeys:ankush, @“TA”, janestudent, @“Student”, nil];
[NSArray arrayWithContentsOfFile:(NSString *)path];
-----创建数组 -----
    //NSArray *array = [[NSArray alloc] initWithObjects:
    @"One",@"Two",@"Three",@"Four",nil];

self.dataArray = array;
    [array release];

//- (unsigned) Count;数组所包含对象个数;
    NSLog(@"self.dataArray cound:%d",[self.dataArray count]);

//- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象;
    NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);

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

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

array1 = [NSArray arrayWithArray:array];
    NSLog(@"array1:%@",array1);

//Copy

//id obj;
    NSMutableArray *newArray = [[NSMutableArray alloc] init];
    NSArray *oldArray = [NSArray arrayWithObjects:
                         @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];

NSLog(@"oldArray:%@",oldArray);
    for(int i = 0; i < [oldArray count]; i++)
    {        
        obj = [[oldArray objectAtIndex:i] copy];
        [newArray addObject: obj];
    }
    //     
    NSLog(@"newArray:%@", newArray);
    [newArray release];

//快速枚举
    //NSMutableArray *newArray = [[NSMutableArray alloc] init];
    NSArray *oldArray = [NSArray arrayWithObjects:
                         @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];   
    NSLog(@"oldArray:%@",oldArray);

for(id obj in oldArray)
    {
        [newArray addObject: obj];
    }
    //     
    NSLog(@"newArray:%@", newArray);
    [newArray release];

//Deep copy

//NSMutableArray *newArray = [[NSMutableArray alloc] init];
    NSArray *oldArray = [NSArray arrayWithObjects:
                         @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];   
    NSLog(@"oldArray:%@",oldArray);   
    newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);
    NSLog(@"newArray:%@", newArray);
    [newArray release];

//Copy and sort

//NSMutableArray *newArray = [[NSMutableArray alloc] init];
    NSArray *oldArray = [NSArray arrayWithObjects:

NSMutableArray
Mutable version of NSArray.
- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(int)index;
- (void)removeObjectAtIndex:(int)index;
- (void)removeAllObjects;

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

-----在数组末尾添加对象-----
    //- (void) addObject: (id) anObject;
    //NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];
    [array addObject:@"Four"];
    NSLog(@"array:%@",array);

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

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

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

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

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

----★使用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:
+ setWithObjects:(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",@"Two",@"2",@"Three",@"3",nil];
    NSString *string = [dictionary objectForKey:@"One"];
    NSLog(@"string:%@",string);
    NSLog(@"dictionary:%@",dictionary);
    [dictionary release];

NSMutableDictionary
Mutable version of NSDictionary.
+ (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"];
    NSLog(@"dictionary:%@",dictionary);
//删除指定的字典
    [dictionary removeObjectForKey:@"3"];
    NSLog(@"dictionary:%@",dictionary);

NSArray,NSSet,NSDictionary总结 (转)相关推荐

  1. IOS 总结:NSArray,NSSet,NSDictionary

    用法总结:NSArray,NSSet,NSDictionary Foundation framework中用于收集cocoa对象(NSObject对象)的三种集合分别是: NSArray 用于对象有序 ...

  2. 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词

    一. 字符串 API 1. NSString 用法简介 (1) NSString API 介绍 NSString 功能 : -- 创建字符串 : 使用 init 开头的实例方法, 也可以使用 Stri ...

  3. 关于NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

    NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary 在 OC 中我们天天都要用,而我们要怎 ...

  4. 容器类的常用操作(NSArray,NSNumber,NSDictionary,NSSet)

    1.数组 : 对象,可为任意类型 //(*1.)NSArray的常用方法NSString *str1 = [[NSString alloc]initWithFormat:@"%d杨毅&quo ...

  5. NSArray、NSDictionary、NSString存储、删改、遍历

    NSString 创建一个NSString实例:NSString *str = @"this is string";//字面量语法 常用API: +stringWithFormat ...

  6. NSArray和NSDictionary

    ----->NSArray 1.创建数组 NSArray *array1 = [[NSArray alloc] initWithObjects:@"One",@"T ...

  7. 放在NSArray、NSDictionary等容器内的对象Item,Item中的property在程序运行过程中被无故释放...

    可能是被释放的property本身是OC对象而它的属性被误写成assign,例如: @interface MyItem : Object @property (nonatomic, assign) N ...

  8. NSArray和NSDictionary的混合

    转载于:https://www.cnblogs.com/xzz5211314/p/5120858.html

  9. 【iOS 开发】Objective-C 运算符

    博客地址 : http://blog.csdn.net/shulianghan/article/details/41624613 参考文章 : 1.[iOS 开发]Object-C 运算符 2.[iO ...

最新文章

  1. SD VC - 变式价格
  2. RedHat 下常见系统故障及恢复方法(整理)
  3. python programming training(一):最大回文子字符串
  4. 【机器学习算法专题(蓄力计划)】二、机器学习中的统计学习方法概论
  5. wxpython bind自定义_wxPython的 - 如何从自定义对话框WX
  6. 服务器红帽操作系统安装,pxe无人值守安装 红帽操作系统
  7. HBase集群环境部署
  8. 突破asp过滤系统入侵网站
  9. php京东接口开发,技术文档
  10. java创建http接口
  11. Spark入门程序(idea,scala)
  12. 网易云解析接口PHP源码,网易云音乐Web端的接口分析
  13. 360无线wifi 无法连接服务器,360wifi连接上但上不了网的解决方法
  14. 京东商品详情查询接口V1新版接口
  15. 驾校考试科目二完整流程
  16. 设计模式 | 观察者模式及典型应用
  17. 722 | Crypto Tech Night第五期分享会,参会通道开放中
  18. 哒哒哒哒哒哒,哒哒哒哒哒哒哒哒
  19. 纸上得来终觉浅(experience is the best teacher)
  20. c语言从键盘输入一个整数判断是否为素数,键盘输入一个正整数,程式设计判断其是否为素数(即质数)...

热门文章

  1. Centos6.8 搭建Tomcat服务器
  2. 关于rabbitmq详细介绍
  3. Apache - AH00341
  4. Visual Studio2005的加载此属性页时出错的解决办法(转)
  5. Linux上安装paramiko模块
  6. JasperReport和jFreeReport的比较
  7. StringUtils工具类的常用方法
  8. osg 改变模型贴图
  9. 2道面试题:输入URL按回车HTTP2
  10. MFC获取文字高宽设置字符间隔