objective-c dictionary(字典) 
 
dictionary是由键-对象组成的数据集合。正如在词典中查找单词的定义一样,可通过对象的键从objective-c词典中获取所需的值。 
 
词典中的键必须是单值的,尽管它们通常是字符串,但还可以是任何对象类型。和键关联的值可以是任何对象类型,但它们不能为nil。 
 
下面是一个使用词典的类: 
 
  
 
view plaincopy to clipboardprint? 
#import <Foundation/NSDictionary.h>   
#import <Foundation/NSObject.h>   
#import <Foundation/NSString.h>   
#import <Foundation/NSAutoreleasePool.h>   
   
int main(int argc, const char *argv[])   
{   
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];   
   
   
   
    //immutable dictionary   
    NSDictionary *tires = [NSDictionary  dictionaryWithObjectsAndKeys :   
                            @"front-left",@"t1" , @"front-right",@"t2" ,   
                            @"back-left",@"t3" , @"back-right",@"t4" ,nil];   
   
    //display immutable dictionary   
    NSLog(@"t1: %@",[tires objectForKey: @"t1"]);   
    NSLog(@"t2: %@",[tires objectForKey: @"t2"]);   
    NSLog(@"t3: %@",[tires objectForKey: @"t3"]);   
    NSLog(@"t4: %@",[tires objectForKey: @"t4"]);   
   
    //mutable dictionary   
    NSMutableDictionary *glossary = [NSMutableDictionary dictionary];   
   
    //Store three entries in the glossary   
    //use setObject:forKey: method to set key/value   
    [glossary setObject: @"A class defined so other classes can inherit from it"   
                 forKey: @"abstract class"];   
    [glossary setObject: @"To implment all the methods defined in a protocol"   
                 forKey: @"adopt"];   
    [glossary setObject: @"Storing an object for later use"   
                 forKey: @"archiving"];   
   
    //Retrieve and display them   
    //use objectForKey:key method to retrieve the value   
    NSLog(@"abstract class: %@",[glossary objectForKey: @"abstract class"]);   
    NSLog(@"adopt: %@",[glossary objectForKey: @"adopt"]);   
    NSLog(@"archiving: %@",[glossary objectForKey: @"archiving"]);   
   
    [pool drain];   
    return 0;   
   
}   
常用的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已存丰,则替换该值 
 
  |   
        
 
    
    
    
     
 
 |