// NSMutableDictionary 增删改查 #import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) { @autoreleasepool {
#pragma mark - NSMutableDictionary
#pragma mark 创建可变字典 //创建字典 NSMutableDictionary *mdict1 = [NSMutableDictionary dictionary]; //空字典 NSMutableDictionary *mdict2 = [NSMutableDictionary dictionaryWithCapacity:3]; //指定长度字典,可变
#pragma mark 添加键值对 // setValue: forKey: [mdict1 setValue:@"liudehua" forKey:@"ldh"]; [mdict1 setValue:@"zhourunfa" forKey:@"zrf"]; [mdict2 setValue:@"zhouxingchi" forKey:@"zxc"]; NSLog(@"%@",mdict1); NSLog(@"%@",mdict2);
#pragma mark 删除键值对 // removeObjectForKey: [mdict1 removeObjectForKey:@"ldh"]; NSLog(@"%@",mdict1);
//删除全部 removeAllObjects [mdict1 removeAllObjects];
#pragma mark 修改字典
//修改 setObject: forKey: [mdict2 setObject:@"zhoujielun" forKey:@"zjl"]; NSLog(@"%@",mdict2);
//简写修改 mdict2[@"zjl"] = @"xxx"; NSLog(@"%@",mdict2);
#pragma mark 查询字典
//查询 //allKeys 查询所有的key NSArray *arr = [mdict2 allKeys];
if ([arr containsObject:@"zxc"]) { NSLog(@"存在zxc的key"); }
#pragma mark - NSDictionary和NSArray对比
//1、NSArray 是有序的,NSDictionary 是无序的 //2、NSArray 是通过下标进行访问,NSDictionary 是通过key访问
#pragma mark NSArray //创建 //@[@"abc",@"def"]; 返回是不可变数组
//访问 // id d = array[1];
//赋值 //array[1] = @"alibaba";
#pragma mark NSDictionary
//1、创建 //@{@"key":@"value",@"name":@"mayun"}; 返回是不可变字典
//2、访问 //id d = dict[@"key"];
//3、赋值 //dict[@"key"]= @"liudehua";
} return 0; }
|