1. 程式人生 > >NSDictionary字典建立,獲取,遍歷,可變字典的刪除

NSDictionary字典建立,獲取,遍歷,可變字典的刪除

字典是以鍵值對的形式來儲存資料 key value

1 NSDictionary 字典

1.1 建立字典,不可變的

NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"xiaozhe",@"name", nil];
NSLog(@"dic %@",dic);
2016-08-14 14:44:17.460 07-字典類[2325:547877] dic {
    name = xiaozhe;
}

1.2 快捷建立方式

NSDictionary * dic2 = @{ @"one":@"1"
,@"two":@"2"}; NSLog(@"dic2 %@",dic2);
2016-08-14 14:44:17.461 07-字典類[2325:547877] dic2 {
    one = 1;
    two = 2;
}

1.3 字典中可以存任意資料型別

字典的順序不是自然順序

NSArray * array = @[@"one",@"two"];

NSDictionary * dic3 = @{
                        @"one":@"1",
                        @"num":[NSNumber numberWithInt:10
], @"aaa":dic2, @"bbb":dic, @"ar1":array }; NSLog(@"dic3 %@",dic3);
016-08-14 14:44:17.461 07-字典類[2325:547877] dic3 {
    aaa =     {
        one = 1;
        two = 2;
    };
    ar1 =     (
        one,
        two
    );
    bbb =     {
        name = xiaozhe;
    }
; num = 10; one = 1; }

1.4 獲得字典的長度

NSLog(@"count %ld",dic3.count);

1.5 從字典中取值

NSString * str  = [dic3 objectForKey:@"one"];
NSLog(@"str %@",str);

NSDictionary * dicTmp = [dic3 objectForKey:@"aaa"];
NSLog(@"dicTmp %@",dicTmp);
NSArray * arrayTmp = [dic3 objectForKey:@"ar1"];
NSLog(@"arrayTmp %@",arrayTmp);

1.6 遍歷

取出所有的key值

NSArray * allkeys = [dic3 allKeys];
NSLog(@"allkeys %@",allkeys);

for (int i = 0; i < allkeys.count; i++)
{
    NSString * key = [allkeys objectAtIndex:i];

    //如果你的字典中儲存的多種不同的型別,那麼最好用id型別去接受它
    id obj  = [dic3 objectForKey:key];
    NSLog(@"obj %@",obj);
}

列舉器

NSEnumerator * enumerator =  [dic3 objectEnumerator];

id value;
while (value = [enumerator nextObject]) {
    NSLog(@"value %@",value);
}

2 NSMutableDictionary 可變字典

2.1 建立一個可變長度字典

NSMutableDictionary * muDic = [[NSMutableDictionary alloc] initWithCapacity:0];

2.2 向字典中儲存資料

[muDic setObject:@"1" forKey:@"one"];
[muDic setObject:@"2" forKey:@"two"];
[muDic setObject:@"3" forKey:@"three"];

2.3 刪除

[muDic removeObjectForKey:@"one"];

2.4 全部刪除

[muDic removeAllObjects];

給一個 Student 類

@interface Student : NSObject

@property (nonatomic,assign) int age;
@property (nonatomic,strong) NSString * name;

- (id)initWithName:(NSString *)name andAge:(int)age;

@end

@implementation Student

- (id)initWithName:(NSString *)name andAge:(int)age
{
    if (self = [super init])
    {
        _name = name;
        _age = age;
    }
    return self;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"name %@ age %d",_name,_age];
}

@end
Student * stu1 = [[Student alloc] initWithName:@"xiaoher" andAge:20];
Student * stu2 = [[Student alloc] initWithName:@"alex" andAge:50];
Student * stu3 = [[Student alloc] initWithName:@"xiaoli" andAge:10];

[muDic setObject:stu1 forKey:@"s1"];
[muDic setObject:stu2 forKey:@"s2"];
[muDic setObject:stu3 forKey:@"s3"];
//在向字典中儲存資料的時候,一定要保證key值是唯一的
//[muDic setObject:stu3 forKey:@"s3"];
//[muDic setObject:stu3 forKey:@"s3"];
//[muDic setObject:stu3 forKey:@"s3"];

2.5 使用for迴圈遍歷字典

NSArray * allkeys = [muDic allKeys];

for (int i = 0; i < allkeys.count; i++)
{
    NSString * key = [allkeys objectAtIndex:i];
    Student * stu = [muDic objectForKey:key];
    NSLog(@"stu %@",stu);
};

2.6 使用列舉器

NSEnumerator * enumerator = [muDic objectEnumerator];
Student * tmp;
while (tmp = [enumerator nextObject]) {
    NSLog(@"tmp %@",tmp);
}