我确定我在想要编写的小型iPhone程序中遗漏了一些内容,但代码很简单,编译时没有任何错误,因此我无法查看错误的位置.
我已经设置了一个NSMutableDictionary来存储学生的属性,每个属性都有一个唯一的密钥.在头文件中,我声明了NSMutableDictonary studentStore:
@interface School : NSObject
{
@private
NSMutableDictionary* studentStore;
}
@property (nonatomic, retain) NSMutableDictionary *studentStore;
Run Code Online (Sandbox Code Playgroud)
当然在实现文件中:
@implementation School
@synthesize studentStore;
Run Code Online (Sandbox Code Playgroud)
我想在字典中添加一个对象:
- (BOOL)addStudent:(Student *)newStudent
{
NSLog(@"adding new student");
[studentStore setObject:newStudent forKey:newStudent.adminNo];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
class Student具有以下属性:@interface Student:NSObject {@private NSString*name; //属性NSString*gender; 年龄; NSString*adminNo; }
其中newStudent具有以下值:Student*newStudent = [[Student alloc] initWithName:@"jane"性别:@"female"年龄:16 adminNo:@"123"];
但是当我查阅字典时:
- (void)printStudents
{
Student *student;
for (NSString* key in studentStore)
{
student = [studentStore objectForKey:key];
NSLog(@" Admin No: %@", student.adminNo);
NSLog(@" Name: %@", student.name);
NSLog(@"Gender: %@", student.gender);
} …Run Code Online (Sandbox Code Playgroud)