鍍金池/ 教程/ C/ Objective-C 數(shù)據(jù)存儲
Objective-C 多態(tài)性
Objective-C 預(yù)處理器
Objective-C for循環(huán)
Objective-C 開發(fā)環(huán)境(安裝配置)
Obj-C Foundation/基礎(chǔ)框架
Objective-C 指針運算
Objective-C 循環(huán)
Objective-C 錯誤處理
Objective-C while循環(huán)
Objective-C if語句
Objective-C do...while循環(huán)
Objective-C教程
Objective-C 嵌套switch語句
Objective-C 函數(shù)按值調(diào)用
Objective-C 常量
Objective-C 運算符
Objective-C 數(shù)據(jù)類型
Objective-C 邏輯運算符
Objective-C 數(shù)組作為函數(shù)參數(shù)傳遞
Objective-C struct/結(jié)構(gòu)
Objective-C 嵌套循環(huán)
Objective-C 函數(shù)引用調(diào)用
Objective-C 日志處理
Objective-C 數(shù)據(jù)存儲
Objective-C 教程首頁
Objective-C 擴展
Objective-C 異常處理
Objective-C 類型轉(zhuǎn)換
Objective-C 嵌套 if 語句
Objective-C typedef
Objective-C 決策
Objective-C 指針的數(shù)組
Objective-C Protocols/協(xié)議
Objective-C 日期和時間
Objective-C 指針
Objective-C 內(nèi)存管理
命令行參數(shù)
Objective-C NSString/字符串
Objective-C 動態(tài)綁定
Objective-C 復(fù)合對象
Objective-C Arrays/數(shù)組
Objective-C 位運算符
Objective-C 指向指針的指針
Objective-C 從函數(shù)返回數(shù)組
Objective-C Posing/冒充
Objective-C Categories/類別
Objective-C 文件處理
Objective-C 賦值運算符
Objective-C 函數(shù)返回指針
Objective-C if...else 語句
Objective-C switch語句
Objective-C URL加載系統(tǒng)
Objective-C 算術(shù)運算符
Objective-C Numbers/數(shù)字
Objective-C語言程序結(jié)構(gòu)
Objective-C 快速枚舉
Objective-C 基本語法
Objective-C 類&對象
Objective-C 變量
Objective-C 關(guān)系運算符
Objective-C 塊
Objective-C break語句
Objective-C continue語句
Objective-C 語言概述
Objective C 文本和字符串
Objective-C 函數(shù)
Objective-C 傳遞函數(shù)的指針
Objective-C 數(shù)組的指針
Objective-C 多維數(shù)組
Objective-C 繼承
Objective-C 數(shù)據(jù)封裝

Objective-C 數(shù)據(jù)存儲

數(shù)據(jù)存儲和檢索是在任何程序中的最重要的。在Objective-C中,我們一般不會依賴于像鏈表結(jié)構(gòu),因為它使復(fù)雜的工作。相反,我們使用的NSArray,NSDictionary,NSSet及其可變形式等的集合。

NSArray & NSMutableArray

NSArray是用來裝不可變的對象數(shù)組,NSMutableArray用于容納一個可變數(shù)組對象。

Mutablility有助于改變數(shù)組中運行一個預(yù)分配的數(shù)組,但如果我們使用NSArray 只能更換現(xiàn)有數(shù)組,并不能改變現(xiàn)有數(shù)組的內(nèi)容。

NSArray 的重要方法如下:

  • alloc/initWithObjects: 用來初始化一個數(shù)組對象。

  • objectAtIndex: 在特定索引allReturns對象。

  • count: 返回的對象的數(shù)量

NSMutableArray繼承自NSArray,因此NSMutableArray有NSArray 所有實例方法

重要的 NSMutableArray 方法如下:

  • removeAllObjects: 清空數(shù)組。

  • addObject: 數(shù)組末尾插入一個給定的對象。

  • removeObjectAtIndex: 這是用來刪除objectAt 指定索引的對象

  • exchangeObjectAtIndex:withObjectAtIndex: 改變陣列中的對象在給定的索引。

  • replaceObjectAtIndex:withObject: 替換的對象與對象在索引。

我們一定要記住,上述列表只是經(jīng)常使用的方法,我們可以在我們的XCode跳進各自的類要知道更多的方法,在這些類。下面是一個簡單的例子。

#import <Foundation/Foundation.h>

int main()
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSArray *array = [[NSArray alloc]
   initWithObjects:@"string1", @"string2",@"string3",nil];
   NSString *string1 = [array objectAtIndex:0];
   NSLog(@"The object in array at Index 0 is %@",string1);
   NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
   [mutableArray addObject: @"string"];
   string1 = [mutableArray objectAtIndex:0];
   NSLog(@"The object in mutableArray at Index 0 is %@",string1); 
   [pool drain];
   return 0;
}

現(xiàn)在,當我們編譯并運行程序,我們會得到以下的結(jié)果。

2013-09-29 02:33:23.195 demo[3487] The object in array at Index 0 is string1
2013-09-29 02:33:23.196 demo[3487] The object in mutableArray at Index 0 is string

在上面的程序中,我們已經(jīng)看到 NSMutableArray 和 NSArray 的一個簡單的區(qū)別,在這里我們可以插入一個字符串分配后可變數(shù)組。

NSDictionary & NSMutableDictionary

NSDictionary 用于容納一個不可變的對象,字典 NSMutableDictionary 用于容納一個可變的對象字典。

重要的NSDictionary方法如下

  • alloc/initWithObjectsAndKeys: 初始化一個新分配的字典帶構(gòu)建從指定的集合值和鍵的條目。

  • valueForKey: 返回與給定鍵關(guān)聯(lián)的值。

  • count: 返回在字典中的條目的數(shù)量。

NSMutableDictionary 繼承自 NSDictionary ,因此NSMutableDictionary 實例擁有 NSDictionary 的所有方法

重要的NSMutableDictionary方法如下:

  • removeAllObjects:清空字典條目。

  • removeObjectForKey: 從字典刪除給定鍵及其關(guān)聯(lián)值。

  • setValue:forKey: 添加一個給定的鍵 - 值對到字典中。

字典的一個簡單的例子如下:

#import <Foundation/Foundation.h>

int main()
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
   @"string1",@"key1", @"string2",@"key2",@"string3",@"key3",nil];
   NSString *string1 = [dictionary objectForKey:@"key1"];
   NSLog(@"The object for key, key1 in dictionary is %@",string1);
   NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc]init];
   [mutableDictionary setValue:@"string" forKey:@"key1"];
   string1 = [mutableDictionary objectForKey:@"key1"];
   NSLog(@"The object for key, key1 in mutableDictionary is %@",string1); 
   [pool drain];
   return 0;
}

現(xiàn)在,當我們編譯并運行程序,我們會得到以下的結(jié)果。

2013-09-29 02:34:50.528 demo[9135] The object for key, key1 in dictionary is string1
2013-09-29 02:34:50.528 demo[9135] The object for key, key1 in mutableDictionary is string

NSSet & NSMutableSet

NSSet 是用來保持一個不變集的不同對象,NSMutableDictionary 用于容納一個可變設(shè)置的不同對象。

重要的NSSet方法如下:

  • alloc/initWithObjects: 初始化一個新分配的成員采取從指定的對象列表。

  • allObjects - 返回一個數(shù)組,包含集合的成員或一個空數(shù)組(如果該組沒有成員)。

  • count: 返回集合中的成員數(shù)量。

NSMutableSet 繼承自NSSet,因此所有NSSet方法的在NSMutableSet 的實例是可用的。

重要的 NSMutableSet方法如下:

  • removeAllObjects: 清空其所有成員的集合。

  • addObject: 添加一個給定的對象的集合(如果它還不是成員)。

  • removeObject: 從集合中刪除給定的對象。

集合的一個簡單的例子如下:

#import <Foundation/Foundation.h>

int main()
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSSet *set = [[NSSet alloc]
   initWithObjects:@"string1", @"string2",@"string3",nil];
   NSArray *setArray = [set allObjects];
   NSLog(@"The objects in set are %@",setArray);
   NSMutableSet *mutableSet = [[NSMutableSet alloc]init];
   [mutableSet addObject:@"string1"];
   setArray = [mutableSet allObjects];
   NSLog(@"The objects in mutableSet are %@",setArray);
   [pool drain];
   return 0;
}

現(xiàn)在,當我們編譯并運行程序,我們會得到以下的結(jié)果。

2013-09-29 02:35:40.221 demo[12341] The objects in set are (string3, string2, string1)
2013-09-29 02:35:40.222 demo[12341] The objects in mutableSet are (string1)