YIem`s Blog -心比天高命比纸薄-链接找不到的请在站内搜索内容!

OC-字典--添加 修改 删除 查询等等

分别有三个文件
.mian.m

int main(int argc, const char * argv[]) {

// 创建学生
Student *stu1 = [[Student alloc]initWithName:@"卞一" sex:@"男" age:@"20"];
Student *stu2 = [[Student alloc] initWithName:@"YIem" sex:@"男" age:@"20"];
Student *stu3 = [[Student alloc]initWithName:@"YI" sex:@"男" age:@"20"];
Student *stu4 = [[Student alloc]initWithName:@"Bian" sex:@"男" age:@"20"];
// 创建学生组
NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:stu1, nil];
NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:stu2, stu3, nil];
NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:stu4, nil];
// 创建字典
NSMutableDictionary *contdic = [NSMutableDictionary dictionaryWithObjectsAndKeys:arr1, @"V", arr2, @"Y", arr3, @"B", nil];
// 添加联系人到匹配的分组
Student *stu5 = [[Student alloc] initWithName:@"BIanYI" sex:@"男" age:@"20"];
// 通过字典找到对应的数组
NSMutableArray *arr5 = [contdic objectForKey:@"B"];
// 将对象添加到对应数组
[arr5 addObject:stu5];
// 创建联系人/数组/Key
Student *stu6 = [[Student alloc]initWithName:@"Bianyi" sex:@"男" age:@"20"];
// 点M里方法实现 这里调用点M
[stu6 addStu:stu6 contdic:contdic];
// forin遍历字典
for (NSString *key in contdic) {
    // 通过key得到数组
    NSMutableArray *arr = [contdic objectForKey:key];
    // 遍历数组
    for (Student *stu in arr) {
        // 得到对象
        [stu info];
    }
}


NSLog(@"%@", contdic.allKeys);
return 0;

}


.Student.h

// 自定义方法

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *sex;
@property (nonatomic, copy) NSString *age;
// 初始化

.Student.m

// 

//

// 创建/添加/修改学生信息


当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »