TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
- //
- // main.m
- // NSMutableDictionary
- //
- // Created by yusian on 14-3-23.
- // Copyright (c) 2014年 小龙虾论坛. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- int main()
- {
- // 创建一个可变字典
- NSMutableDictionary *dict = [NSMutableDictionary dictionary];
-
- // 添加键值对
- [dict setObject:@"yusian" forKey:@"name"];
-
- [dict setObject:@"ChangSha" forKey:@"address"];
-
- // 移除键值对
- // [dict removeObjectForKey:(id)];
- // [dict removeAllObjects];
- // [dict removeObjectsForKeys:(NSArray *)];
-
- // 字典打印
- NSLog(@"%@", dict);
-
- // 字典的遍历之for循环
- for (int i = 0; i < dict.count; i++) {
-
- // 先取出所有的key
- NSArray * keys = [dict allKeys];
-
- // 每次循环都从数组中取出一个key值
- NSString *key = keys[i];
-
- // 根据每次取出的key值,找到对应的键值
- NSString *obj = dict[key];
-
- // 输出
- NSLog(@"%@ - %@", key, obj);
- }
-
- // 字典遍历之遍历方法(传block方式),具体实现在数组的遍历方法中有详细研究过
- [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
-
- // 可直接使用key与obj
- NSLog(@"%@ - %@", key, obj);
-
- // 如果key值等于name则遍历终止
- if ([key isEqualToString:@"name"]) {
-
- *stop = YES;
-
- }
- }];
- return 0;
- }
复制代码
|
|