可變字典使用舉例:
設計一個學生類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