Objective-C ---NSArray(梳理整理)

NSArray

NSArray 和NSDictionary都是oc中的集合類,什麼叫作集合,就是裝對象的對象html

NSArray是有序的 NSDictionary是無序的數組

1.建立方式   a.[[NSArray alloc] initWithArray:arr1];(用自己建立)xcode

                    b.NSArray*array = @[對象1,對象2,。。。。];app

2.經常使用的屬性dom

    a.count   獲取個數函數

    b.array[0] 獲取第0個元素學習

    c.contains 是否包含 ,返回BOOL    類型ui

    d.subarrayWithRange  提取元素atom

3.遍歷  1.for(,,)2.enum ([enum1 nextObject]爲nil 的時候中止)3.for in(***)spa

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(向元素髮信息)

  1. makeObjectsPerformSelector:
  2. makeObjectsPerformSelector:withObject:
  3. enumerateObjectsUsingBlock:
  4. enumerateObjectsWithOptions:usingBlock:    
  5. enumerateObjectsAtIndexes:options:usingBlock:

1.6 Comparing Arrays(比較Array)

1.7 Deriving New Arrays(派生新的Array)

  1. arrayByAddingObject:
  2. arrayByAddingObjectsFromArray:
  3. filteredArrayUsingPredicate:
  4. subarrayWithRange:

1.8 Sorting(排序)

  1. sortedArrayHint
  2. sortedArrayUsingFunction:context:
  3. sortedArrayUsingFunction:context:hint:
  4. sortedArrayUsingDescriptors:
  5. sortedArrayUsingSelector:
  6. sortedArrayUsingComparator:
  7. sortedArrayWithOptions:usingComparator:

1.9 Working with String Elements(和NSString 聯繫)

  1. componentsJoinedByString:

1.10 Creating a Description(從新description 建立一個描述)

1.11 Storing Arrays(儲存)

  1. writeToFile:atomically:
  2. writeToURL:atomically:

1.12 Collecting Paths

1.13 Key-Value Observing(KVO)

1.14 Key-Value Coding(KVC)

1.15 Randomly Shuffling an Array(隨機洗牌)

  1. shuffledArray
  2. 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(增)

  1. addObject:
  2. addObjectsFromArray:
  3. insertObject:atIndex:
  4. insertObjects:atIndexes:

1.2 Removing Objects(刪)

1.3 Replacing Objects(改)

1.4 Filtering Content(內容過濾)

  1. filterUsingPredicate:

1.5 Rearranging Content(從新安排內容)

  1. exchangeObjectAtIndex:withObjectAtIndex:
  2. sortUsingDescriptors:
  3. sortUsingComparator:
  4. sortWithOptions:usingComparator:
  5. sortUsingFunction:context:
  6. sortUsingSelector:

1.6 Initializers

Relationships

Inherits From   NSArray

相關文章
相關標籤/搜索