NSArray
NSArray 和NSDictionary都是oc中的集合类,什么叫做集合,就是装对象的对象
NSArray是有序的 NSDictionary是无序的
1.创建方式 a.[[NSArray alloc] initWithArray:arr1];(用本身创建)
b.NSArray*array = @[对象1,对象2,。。。。];
2.常用的属性
a.count 获取个数
b.array[0] 获取第0个元素
c.contains 是否包含 ,返回BOOL 类型
d.subarrayWithRange 提取元素
3.遍历 1.for(,,)2.enum ([enum1 nextObject]为nil 的时候停止)3.for in(***)
4.排序方法
1.sortedArrayUsingFunction{传入返回类型为NSInteger的函数名,并实现这个函数,较麻烦}
2.sortedArrayWithOptions:<#(NSSortOptions)#> usingComparator:<#^NSComparisonResult(id obj1, id obj2)cmptr#>系统封装好的一个方法,差不多就是sortedArrayUsingFunction的函数方法写在Block内
3.NSSortDescriptor(***)
NSSortDescriptor *des1 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];//根据name排列
NSSortDescriptor *des2 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];//根据age排列
NSArray *desArr = @[des1,des2];//当des1相同时 再比较des2
NSArray *newArray = [array sortedArrayUsingDescriptors:desArr];
子类:NSMutableArray用的比较多
增: add(在后面添加新元素), insert(讲新元素插入指定下标)
删: remove*****
改: replace****, exchange**** , set***** (全部改为....)
查:即上面写的遍历方法
======================================
NSDictionary
NSDictionary是无序的集合(NSArray是有序的)
key->value键值对,也就是说字典是通过key来找值的( NSArray是通过下标找值的)
1.创建方式
a.initWithDictionary
b.[NSDictionary dictionaryWithObjects:<#(NSArray *)#> forKeys:<#(NSArray *)#>
c.NSDictionary* dictionary = @{key1 : value1 , key2 : value2 , key3 : value3 ……}<<NSArray为[]<-中括号>>
2.常用的属性
a.count
b.id obj = dictionary[key]
c.allValues, allKeysForObject
3.遍历 直接推荐forin方式
子类NSMutableDictionary
增: set***** 对键值对(一般就这个了); add***** 增加一个字典内容;
删: remove*******
改: set******(和增加是同一个方法,有的时候就修改value 没有的话就会添加一对键值对)
=========================================
比较
1.字典相对与数组的优点 (查询)
字典获取元素速度比数组快
2.获取元素的方式
数组通过下标获取指定元素的内容
字典通过key来获取指定元素的内容
3.遍历删除的问题 ?? forin enum block 均不能在遍历的同时 删除元素 for 可以,条件设置为i--
苹果官方文档学习(部分在这 全部请至官文文档查看)
class NSArray
NSArray and its subclass NSMutableArray manage ordered collections of objects called arrays. NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects.
NSArray和其子类的对象称为nsmutablearray管理有序数组集合。NSArray创建静态数组和动态数组,nsmutablearray创建。当需要对象的有序集合时,可以使用数组。
Overview
NSArray is “toll-free bridged” with its Core Foundation counterpart, CFArrayRef. See Toll-Free Bridging for more information on toll-free bridging.
Symbols
1.1 Creating an Array(类方法创建)
1.2 Initializing an Array(对象方法创建)
1.3 Querying an Array(查询一个数组)
1.4 Finding Objects in an Array(查找一个对象)
1.5 Sending Messages to Elements(向元素发信息)
- makeObjectsPerformSelector:
- makeObjectsPerformSelector:withObject:
- enumerateObjectsUsingBlock:
- enumerateObjectsWithOptions:usingBlock:
- enumerateObjectsAtIndexes:options:usingBlock:
1.6 Comparing Arrays(比较Array)
1.7 Deriving New Arrays(派生新的Array)
- arrayByAddingObject:
- arrayByAddingObjectsFromArray:
- filteredArrayUsingPredicate:
- subarrayWithRange:
1.8 Sorting(排序)
- sortedArrayHint
- sortedArrayUsingFunction:context:
- sortedArrayUsingFunction:context:hint:
- sortedArrayUsingDescriptors:
- sortedArrayUsingSelector:
- sortedArrayUsingComparator:
- sortedArrayWithOptions:usingComparator:
1.9 Working with String Elements(和NSString 联系)
- componentsJoinedByString:
1.10 Creating a Description(重新description 创建一个描述)
1.11 Storing Arrays(储存)
- writeToFile:atomically:
- writeToURL:atomically:
1.12 Collecting Paths
1.13 Key-Value Observing(KVO)
1.14 Key-Value Coding(KVC)
1.15 Randomly Shuffling an Array(随机洗牌)
- shuffledArray
- shuffledArrayWithRandomSource:
1.16 New Methods(initWithCoder:)
1.17 Constants
Relationships
2.1 Inherits From NSObject
2.2 Conforms To NSCopying,NSMutableCopying,NSFastEnumeration ,NSSecureCoding
class NSMutableArray
Symbols
Creating and Initializing a Mutable Array
1.1 Adding Objects(增)
- addObject:
- addObjectsFromArray:
- insertObject:atIndex:
- insertObjects:atIndexes:
1.2 Removing Objects(删)
1.3 Replacing Objects(改)
1.4 Filtering Content(内容过滤)
- filterUsingPredicate:
1.5 Rearranging Content(重新安排内容)
- exchangeObjectAtIndex:withObjectAtIndex:
- sortUsingDescriptors:
- sortUsingComparator:
- sortWithOptions:usingComparator:
- sortUsingFunction:context:
- sortUsingSelector:
1.6 Initializers
Relationships
Inherits From NSArray