Objective-c学习笔记06——字典与集合

Objective-C可以开发apple家族系列产品的软件,这里记录成笔记方便日后复习:

一、词典

1、词典(dictionary)是由键——对象对组成的数据集合。在与词典中超找单词定义一样,可以通过对象的键从Objective-c词典中获取需要的值(即那个对象)。词典中的键必须是单值的,通常他们是字符串,但也可以是其他对象类型。和键关联的值可以是任意对象类型,但不能是nil。
2、代码

//1.dictionary  创建一个空字典
NSDictionary * dic = [NSDictionary dictionary];

//2. 创建并初始化
//  dictionaryWithObject:(id)forkey:(id)<nscopying>
NSDictionary * dic1 = [NSDictionary dictionaryWithObject:@"Apple" forKey:@"Android"];
NSLog(@"%@", dic1);

//3.创建并返回一个字典。
// dictionaryWithObjects:forkeys
NSArray * keys = [NSArray arrayWithObjects:@"name", @"sex", @"age", nil];
NSArray * values = [NSArray arrayWithObjects:@"Apple", @"女", [NSNumber numberWithInt:25], nil];
NSDictionary * dic3 = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSLog(@"%@", dic3);

//4.使用键-对象对{key1,obj1}、{key2,obj2}...创建词典
//  dictionaryWithObjectsAndKeys
NSDictionary * dic4 = [NSDictionary dictionaryWithObjectsAndKeys:
                       @"apple", @"name",
                       @25,      @"age", nil];
NSLog(@"%@", dic4);

//5. @ 自面量
NSDictionary * dic5 = @{@"name": @"Apple", @"age": @25, @"sex": @"女"};
NSLog(@"@ = %@", dic5);

// allkeys 返回一个数组包含词典中所有的键
NSLog(@"allkeys:%@", [dic5 allKeys]);

//allkeysForObject: 返回一个数组,包含所有对应到给定对象的关键字。
NSArray * aa = @[@"name",@"apple",@"winphone",@"apple"];
NSLog(@"aaaaaaa%@", [dic5 allKeysForObject:aa]);


//allValues:返回一个数组包含词典中所有的值
NSLog(@"allValues:%@", [dic5 allValues]);

//objectForKey:返回指定 key 的 value,若没有这个 key 返回 nil.
NSLog(@"%@", [dic5 objectForKey:@"name"]);

//valueForKey 同样是返回指定 key 的 value。
NSLog(@"%@", [dic5 valueForKey:@"name"]);

//objectsForKeys:< #(NSArray *)#> notFoundMarker:< #(id)#>
// 根据给定的键值数组返回对应的值,如果找不到则返回 notFoundMarker
NSLog(@"%@", [dic5 objectsForKeys:@[@"Name", @"Age", @"Sex"] notFoundMarker:@"NOT"]);


//count:返回字典中的记录数
NSLog(@"count:%zi", [dic5 count]);

//遍历字典中的key
NSLog(@"遍历字典\n\n");
//迭代器遍历
NSEnumerator * keyEnum = [dic5 keyEnumerator];
id key = nil;
while (key = [keyEnum nextObject]) {
    NSLog(@"%@", key);
}

//快速遍历
for (id key in dic5) {
    NSLog(@"%@", key);
}
//遍历字典中的value
for (id key in dic5) {
    NSLog(@"%@", [dic5 valueForKey:key]);
}

//block遍历
[dic5 enumerateKeysAndObjectsUsingBlock:
 ^(id key, id obj, BOOL *stop) {
     NSLog(@"%@ = %@", key, values);
 }];


//可变字典
NSMutableDictionary * dic2 = [NSMutableDictionary dictionary];

//setObject: forKey: 添加value与key,如果key存在则替换该值
[dic2 setValue:@"HH" forKey:@"100"];
[dic2 setValue:@"kj" forKey:@"哈哈"];
NSLog(@"%@", dic2);

//removeObjectForKey 删除词典中指定key对应的记录
[dic2 removeObjectForKey:@"100"];
NSLog(@"%@", dic2);

//removeAllObjects 删除词典中的所有记录
[dic2 removeAllObjects];
NSLog(@"%@", dic2);

NSDictionary * diction1 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"Apple", @"Name",
                           @15, @"Age",nil];
NSDictionary * diction2 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"Android", @"Name",
                           @28, @"Age",nil];

//isEqualToDictionary 比较两个字典
if([diction1 isEqualToDictionary:diction2]){
    NSLog(@"diction1 equal diction2");
}else{
    NSLog(@"diction1 not equal diction2");
}


// writeToFile 把字典写入文件(xml格式)
NSString * path = @"/Users/simman/diction1.xml";
[diction1 writeToFile:path atomically:YES];

//dictionaryWithContentsOfFile 读取一个文件返回给新的字典对象
NSDictionary *newDic = [NSDictionary dictionaryWithContentsOfFile:path];
</nscopying>