您可以编写学生和班级课程。 NSDictionary 可以用两个 Key-Pair 值(一个用于 Marks,一个用于 cmets)来描述主题。 Student 可以有一个 NSArray 类型的实例 var(这将是一个主题字典的数组)。类可以有一个 NSArray 类型的实例变量(这将是一个 Student 对象的数组)
类.h
@interface Class : NSObject
@property (nonatomic, strong) NSMutableArray *students;
@end
类.m
-(id)initWithStudents:(NSMutableArray *)newStudents{
if(self = [super init]) {
self. students = newStudents
}
return self;
}
学生.h
@interface Student : NSObject
@property (nonatomic, strong) NSMutableArray *subjects;
@end
学生.m
-(id)initWithStudents:(NSMutableArray *)newSubjects{
if(self = [super init]) {
self.subjects = newSubjects
}
return self;
}
按如下方式创建您的 Subjects、Student 和 Class 对象:
NSMutableDictionary *subject1 = [NSMutableDictionary dictionaryWithObjects:@[@"80", @"Very Good"] forKeys:@[@"Marks", @"Comments"]];
NSMutableDictionary *subject2 = [NSMutableDictionary dictionaryWithObjects:@[@"70", @"Good"] forKeys:@[@"Marks", @"Comments"]];
NSMutableDictionary *subject3 = [NSMutableDictionary dictionaryWithObjects:@[@"40", @"Poor"] forKeys:@[@"Marks", @"Comments"]];
Student *student1 = [[Student alloc] initWithSubjects:@[subject1, subject2, subject3]];
//Create student2 and student 3 in similar fashion.
Class *class1 = [[Class alloc] initWithStudents:@[srudent1, student2, student3]];