可变字典使用举例:
设计一个学生类Student, 有这些属性:name(姓名)、age(年龄)、score(分数)、(classNum)班级
(1)将如下学生添加到数组中
姓名年龄分数班级
Tom1782Class01
Jim2275Class01
Jerry3454Class01
Owen2298Class04
Steve1977Class05
(2)计算所有学生的平均分(年级的平均分)
(3)计算各个班级的平均分
(4)用名字作为key, value是学生对象,将这些学生存入字典。
main.m-------------------------------------------------------
int main(int argc, const char * argv[]) {
@autoreleasepool {
/* 第1步:添加学生到数组中 */
// 创建学生类-构建对象
// Tom
Student *tom = [[Student alloc] initWithName:@"Tom"
WithAge:17
WithScore:82
WithClassBan:@"Class01"];
// Jim
Student *jim = [[Student alloc] initWithName:@"Jim"
WithAge:22
WithScore:75
WithClassBan:@"Class01"];
// Jerry
Student *jerry = [[Student alloc] initWithName:@"Jerry"
WithAge:34
WithScore:54
WithClassBan:@"Class01"];
// Owen
Student *owen = [[Student alloc] initWithName:@"Owen"
WithAge:22
WithScore:98
WithClassBan:@"Class04"];
// Steve
Student *steve = [[Student alloc] initWithName:@"Steve"
WithAge:19
WithScore:77
WithClassBan:@"Class05"];
// 将学生添加入数组
NSMutableArray *arrayStudent = [NSMutableArray arrayWithObjects:tom, jim, jerry, owen, steve, nil];
//计算所有学生的平均分(年级的平均分)
CGFloat sum = 0;
for (Student *stu in arrayStudent) {
sum += stu.score;
}
NSLog(@"平均分:%.2f", sum / arrayStudent.count);
//计算各个班级的平均分
//字典
/*
{
@"class1": [tom, jerry]}
@"class2": [keven, jobs]}
}
*/
NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
for (Student *stu in arrayStudent) {
//从学生中获取学生所在的班级
NSString *classNum = stu.classNum;
NSMutableArray *mArr = [mDic objectForKey:classNum];
//判断可变数组是否存在,如果不存在,则初始化此数组
if (mArr == nil) {
mArr = [NSMutableArray array];
[mDic setObject:mArr forKey:classNum];
}
//把学生添加到可变数组中
[mArr addObject:stu];
}
NSLog(@"mDic is %@", mDic);
//通过遍历字典 算出每个班的平均分
for (NSString *key in mDic) {
NSMutableArray *mArr = mDic[key];
CGFloat sum = 0;
for (Student *stu in mArr) {
sum += stu.score;
}
NSLog(@"%@班级的平均分为%.2f", key, sum / mArr.count);
}
// 用名字作为key, value是学生对象,将这些学生存入字典。
NSMutableDictionary *stuDic = [NSMutableDictionary dictionary];
for (Student *stu in arrayStudent) {
NSString *key = stu.name;
[stuDic setObject:stu forKey:key];
}
NSLog(@"stuDic is %@", stuDic);
}
return 0;
}
Student.h(学生类)-----------------------------------------------------------------------
@interface Student : NSObject
// 定义学生属性
{ // 姓名
NSString *_name;
// 年龄
NSInteger _age;
// 分数
NSInteger _score;
// 班级
NSString *_classNum;
}
// 定义方法
#pragma mark -(自定义初始化方法)
- (id)initWithName:(NSString *)name
WithAge:(NSInteger)age
WithScore:(NSInteger)score
WithClassBan:(NSString *)classNum;
#pragma mark -Set
#pragma mark -Get
- (NSString *)name;
- (NSInteger)age;
- (NSInteger)score;
- (NSString *)classNum;
@end
Student.m------------------------------------------------------------------------------
#import "Student.h"
@implementation Student
#pragma mark -(自定义初始化方法)
- (id)initWithName:(NSString *)name
WithAge:(NSInteger)age
WithScore:(NSInteger)score
WithClassBan:(NSString *)classNum
{
self = [super init];
if (self) {
//..
_name = name;
_age = age;
_score = score;
_classNum = classNum;
}
return self;
}
#pragma mark -Set
#pragma mark -Get
- (NSString *)name
{
return _name;
}
- (NSInteger)age
{
return _age;
}
- (NSInteger)score
{
return _score;
}
- (NSString *)classNum
{
return _classNum;
}
@end