NSArray与NSSet
NSArray:有序的集合,存储的元素在一个整块的内存中并按序排列;
NSSet:无序的集合,散列存储。
读developer.apple关于NSSet的解释:You can use sets as an alternative to arrays when the order of elements isn’t important and performance in testing whether an object is contained in the set is a consideration—while arrays are ordered, testing for membership is slower than with sets.
就是说,如果搜索一个元素,NSSet的效率会比NSArray高。为什么呢?道理比较简单:hash!NSSet中元素的存储和访问都是一个hash的过程。比如你要存储元素A,一个hash算法直接就能直接找到A应该存储的位置;同样,当你要访问A时,一个hash过程就能找到A存储的位置。而对于NSArray,若想知道A到底在不在数组中,则需要一个一个元素比较,显然效率没了。

循环使用整个NSArray内的对象

循环使用整个NSArray内的对象是非常常用的了,而且最近我在研究究竟怎么能方便的把NSArray存入Core Data,所以这更是必要了,看看如下的方法吧:

1,Objective-C 2.0法,最应该使用的

NSArray *aArray; //我们的Array,假设他已经初始化有内容了
for(id innerObj in aArray) //id可以由其他对象类型替代
{//也就是说, 在循环中的参数innerObj就是aArray中的对象
//由于NSArray中只能储存对象,所以我们使用id作为内涵对象的类型,其实就是个指针了
//而且如果NSArray中的内容不一致,id更不会出现冲突
}

2,C的老方法,不推荐,低性能

NSArray *aArray; //我们的Array,假设他已经初始化有内容了
for(int i=0;i<[aArray count]; i++)
{[[aArray objectAtIndex:i] 然后作你想作的事情]; //做爱 作的事情...
}

3, 用NSEnumerator(不知道怎么翻译阿….)

 NSEnumerator* myIterator = [myArray reverseObjectEnumerator];id anObject;while( anObject = [myIterator nextObject]) //每次读取“逐读器”的下一个对象{//anObject和刚才的innerObj是一个意思,但是看看多了这么多行}

三种集合类来收集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)makeObjectsPerformSelect or:(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:
+ dictionaryWithObjectsAnd Keys: (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相关推荐

  1. NSString NSArray NSDictionary NSSet 中的部分方法

    NSString NSArray NSDictionary NSSet 中的部分方法 //1.init     //    NSString  *s1 = [[NSString alloc]init] ...

  2. NSArray与NSSet的区别

    转自:http://maccrazy.diandian.com/post/2011-10-09/5671451 NSArray和NSMutableArray无需多说,是线性数组和链表的包装类. NSS ...

  3. NSSet与NSArray区别

    NSSet与NSArray区别   NSSet到底什么类型,其实它和NSArray功能性质一样,用于存储对象,属于集合: NSSet  , NSMutableSet类声明编程接口对象,无序的集合,在内 ...

  4. Objective-C学习篇08—NSDictionary与NSSet

    Objective-C学习篇08-NSDictionary与NSSet NSDictionary与NSMutableDictionary NSSet与NSMutableSte 字典 字典:字典分为可变 ...

  5. [深入浅出Cocoa]详解键值观察(KVO)及其实现机理

    [深入浅出Cocoa]详解键值观察(KVO)及其实现机理 本文转载至    罗朝辉 (http://www.cppblog.com/kesalin/) 本文遵循"署名-非商业用途-保持一致& ...

  6. NSPredicate

    一.简介 NSPredicate指定数据被获取或者过滤的方式,是一个Foundation类. Cocoa框架中的NSPredicate用于指定过滤器的条件(即查询),它的原理和用法都像SQL的WHER ...

  7. iOS 中KVC、KVO、NSNotification、delegate 总结及区别

    iOS 中KVC.KVO.NSNotification.delegate 总结及区别 1.KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属 ...

  8. 数据持久化 技术比较

      [数据存储]coredata.sqlite.fmdb和sqlitepersistentobject 管理提醒: 本帖被 angellixf 从 OS X开发讨论区 移动到本区(2014-02-12 ...

  9. zzz KVC/KVO原理详解及编程指南

    前言: 1.本文基本不讲KVC/KVO的用法,只结合网上的资料说说对这种技术的理解. 2.由于KVO内容较少,而且是以KVC为基础实现的,本文将着重介绍KVC部分. 一.简介 KVC/KVO是观察者模 ...

最新文章

  1. 西瓜书_阶段1_作业参考
  2. [bug]不包含“AsNoTracking”的定义
  3. 2.9 迁移学习-深度学习第四课《卷积神经网络》-Stanford吴恩达教授
  4. C#软件开发实例.私人订制自己的屏幕截图工具(十)在截图中包括鼠标指针形状...
  5. C#使用Xamarin开发可移植移动应用(4.进阶篇MVVM双向绑定和命令绑定)附源码
  6. 干货|MIT线性代数课程精细笔记5
  7. idea数据库反向生成实体类_IntelliJ IDEA 的数据库管理工具实在太方便了
  8. React组件-事件、状态和生命周期
  9. 通向财务自由之路09_知道何时收手:如何保护你的资本
  10. java开辟_仿照百度文库_FlexPaper
  11. windows环境下安装TensorFlow(亲测)
  12. webpack最佳入门实践系列(08)
  13. UML统一建模语言——类图、时序图
  14. matlab gui界面设计 打开文件,matlab GUI界面设计 点击按钮加载.mat数据的所有变量到工作空间中...
  15. 三菱PLC开发环境搭建笔记
  16. youtube的使用体会
  17. Mac 升级Python 有python2.7到python3.9
  18. html5 堆栈不足,超简单!不用PS也能玩堆栈摄影
  19. 参考基因组 坐标转换 hg38 hg19
  20. swift 判断是否设置了代理

热门文章

  1. git仓库maven项目,添加到myeclipse
  2. 网络游戏和视频体验度量
  3. 阿里技术专家:支付宝超级 App 的弹性动态架构实践
  4. C语言能实现汇编语言的大部分功能,C语言并不能实现汇编语言的大部分功能
  5. 学编程的用什么软件比较好,你知道吗?
  6. win10系统numba.cuda报错
  7. Django学习 day74之BBS项目day3
  8. 从中报看融创为什么是一家好公司?
  9. 李开复博士------美国大学启示录
  10. 20230307 Mac M1 install open-source pymol-安装开源版本pymol