objective-c dictionary(字典)


dictionary是由鍵-對象組成的數據集合。正如在詞典中查找單詞的定義一樣,可通過對象的鍵從objective-c詞典中獲取所需的值。

詞典中的鍵必須是單值的,盡管它們通常是字符串,但還可以是任何對象類型。和鍵關聯的值可以是任何對象類型,但它們不能為nil。

下面是一個使用詞典的類:

 

  1. #import <Foundation/NSDictionary.h>  
  2. #import <Foundation/NSObject.h>  
  3. #import <Foundation/NSString.h>  
  4. #import <Foundation/NSAutoreleasePool.h>  
  5.   
  6. int main(int argc, const char *argv[])  
  7. {  
  8.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  9.   
  10.   
  11.   
  12.     //immutable dictionary  
  13.     NSDictionary *tires = [NSDictionary  dictionaryWithObjectsAndKeys :  
  14.                             @"front-left",@"t1" , @"front-right",@"t2" ,  
  15.                             @"back-left",@"t3" , @"back-right",@"t4" ,nil];  
  16.   
  17.     //display immutable dictionary  
  18.     NSLog(@"t1: %@",[tires objectForKey: @"t1"]);  
  19.     NSLog(@"t2: %@",[tires objectForKey: @"t2"]);  
  20.     NSLog(@"t3: %@",[tires objectForKey: @"t3"]);  
  21.     NSLog(@"t4: %@",[tires objectForKey: @"t4"]);  
  22.   
  23.     //mutable dictionary  
  24.     NSMutableDictionary *glossary = [NSMutableDictionary dictionary];  
  25.   
  26.     //Store three entries in the glossary  
  27.     //use setObject:forKey: method to set key/value  
  28.     [glossary setObject: @"A class defined so other classes can inherit from it"  
  29.                  forKey: @"abstract class"];  
  30.     [glossary setObject: @"To implment all the methods defined in a protocol"  
  31.                  forKey: @"adopt"];  
  32.     [glossary setObject: @"Storing an object for later use"  
  33.                  forKey: @"archiving"];  
  34.   
  35.     //Retrieve and display them  
  36.     //use objectForKey:key method to retrieve the value  
  37.     NSLog(@"abstract class: %@",[glossary objectForKey: @"abstract class"]);  
  38.     NSLog(@"adopt: %@",[glossary objectForKey: @"adopt"]);  
  39.     NSLog(@"archiving: %@",[glossary objectForKey: @"archiving"]);  
  40.   
  41.     [pool drain];  
  42.     return 0;  
  43.   
  44. }  

常用的NSDictionary方法:

+(id) dictionaryWithObjectsAndKeys:                      使用鍵-對象{key1,obj1}、{key2,obj2}...創建詞典

obj1,key1,obj2,key2,...,nil;

-(id) initWithObjectsAndKeys:                                 將新分配的詞典初始化為鍵-對象對{key1,obj1}{key2,obj2}...創建詞典

obj1,key1,obj2,key2...,nil;

-(unsigned int) count                                              返回詞典中的記錄數

-(NSEnumerator *) keyEnumerator                         為詞典中所有鍵返回一個NSEnumerator對象

-(NSArray *) keysSortedByVlaueUsingSelector:      返回詞典中的鍵數組,它根據selector 指定的比較方法進行了排序

(SEL) selector

-(id) objectForKey:key                                             返回指定key的對象

常用的NSMutableDictionary方法:

+(id) dictionaryWithCapacity:size              使用一個初始指定的size創建可變詞典

-(id) initWithCapacity:size                          將新分配的詞典初始化為指定的size

-(void) removeAllObjects                           刪除詞典中所有的記錄

-(void) removeObjectForKey:key              刪除詞曲中指定key對應的記錄

-(void) setObject: obj forKey: key            向詞典為key 鍵添加obj,如果key已存豐,則替換該值

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM