- 浏览: 262226 次
- 性别:
- 来自: 大连
文章分类
最新评论
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法,最应该使用的
2,C的老方法,不推荐,低性能
NSArray *aArray; //我们的Array,假设他已经初始化有内容了
3, 用NSEnumerator(不知道怎么翻译阿….)
三种集合类来收集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:
-----创建数组 -----
------ 从一个数组拷贝数据到另一数组(可变数级) -------
NSMutableArray
Mutable version of NSArray.
-----给数组分配容量-----
-----在数组末尾添加对象-----
-----删除数组中指定索引处对象-----
----- 数组枚举-----
----- NSValue(对任何对象进行包装)-----
----★使用NSMutableArray要防止内存泄露★------
由此可知,每次执行上面的数组操作后,要执行对象release,如上面注释中的语句,才能保证内存不泄露。
NSSet
Unordered collection of objects.
Immutable. You cannot add or remove objects to it once it’s created.
Important methods:
NSMutableSet
Mutable version of NSSet.
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.
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)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 以及他们子类的说明
NSArray,NSSet,NSEnumerator,NSDictionary及对应Mutable介绍,有对应的实例代码
这是NSArray,NSSet,NSEnumerator,NSDictionary及对应Mutable介绍的示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7884218
每个返回NSArray或NSSet都会创建一个新对象,而不会改变原始对象。 如果您使用的是 ARC,则需要为这些文件关闭它,直到它们支持 ARC。 在 Target Settings -> Build Phases -> Compile Sources 下,找到 NSArray+...
一些NSArray,NSDictionary,NSSet相关的算法知识1
NSDictionary,NSArray,NSSet和NSString尚未隐式转换为Dictionary,Array,Set和String。 为了在这些类型之间进行转换,我们已向这些类型临时添加了一个协议,以允许对其进行转换。 有一种方法称为bridge() 。 ...
主要介绍了iOS集合遍历(NSArray、NSDictionary、NSSet)的方法,文中给出了详细的方法示例,并总结了各个方法的优缺点来供大家学习参考,需要的朋友们下面来一起看看吧。
Objective C数组的内存布局要了解NSArray,NSSet,NSDictionary这些集合类的使法,我们需要先弄明其对应的内存布局(Memory L
这些宏为您提供了与其他语言中的集合文字相似的内容,而不是Cocoa提供的笨拙的[NSArray arrayWithObjects:...]语法。 ARRAY(a, b, c) -创建一个包含对象a,b和c的NSArray 。 SET(a, b, c) -类似于ARRAY ,但是创建...
## MCSCollectionUtility NSArray , NSSet和NSDictionary类别提供了许多使收集操作更容易和更干净的方法。 ##安装要在您的应用程序中使用MCSCollectionUtility,只需将“ Classes”文件夹拖到yout项目中,然后...
该库使Objective-C集合对象(例如NSDictionary , NSArray , NSSet和NSOrderSet )返回类似于JSON和Unicode的字符串。 在Xcode控制台中使用NSLog()或与LLDB或GDB进行交互以打印集合对象时,这将很有帮助。 如果您...
应用程序基础框架1157.1 字符串处理1157.2 容器1197.2.1 NSArray1197.2.2 NSDictionary1217.2.3 NSSet1237.2.4 NSIndexSet1247.3 运行循环和定时器1257.4 通告1287.5 程序包1317.6 处理文件1327.6.1 ...
NSArray NSBundle NSData NSDate NSDictionary NSException NSFileManager NSObject NSSet NSString NSTimer NSURL UIKit UIBezierPath UIButton UIColor UIDevice UIImage UIImageView UILable ...
它为您提供了一组关于NSArray,NSDictionary和NSSet的扩展方法,以帮助您处理常见的操作(通常以功能方式)。 文献资料 完整文档可在获得。 使用CocoaPods安装 是Swift和Objective-C Cocoa项目的依赖项管理器。 您...
但是,苹果对于我们的NSDictionary、NSSet、NSArray等值有中文时,打印出来的是Unicode编码,人类无法直接读懂,因此,笔者研究研究如何将打印出来的日志保持原有的格式化且能够将Unicode编码打印出来是正常人类可读...
文档中包括: NSNumber,NSArray,NSString,NSDictionary,NSSet 属性以及相关方法
支持的基础集合包括: NSArray , NSDictionary , NSOrderedSet和NSSet 。功能运算符过滤循环访问集合,并返回包含满足条件的元素的数组。 NSArray < NSNumber> *filteredArray = [@[ @ 13 , @ 42 , @ 0 ] fbl_...
【无限互联】iOS开发视频教程课件第四章Foundation框架课件是由无限互联提供的iOS开发视频教程对应的课件,课件非常详细的讲解了Foudation框架里面的...包括: NSNumber、NSString、NSArray、NSDictionary和NSSet等。