我确信我在尝试编写的一个小型iPhone程序中遗漏了一些东西,但是代码很简单并且编译时没有任何错误,所以我无法看出错误在哪里。
我设置了一个 NSMutableDictionary 来存储学生的属性,每个属性都有一个唯一的键。在头文件中,我声明 NSMutableDictonary StudentStore:
@interface School : NSObject
{
@private
NSMutableDictionary* studentStore;
}
@property (nonatomic, retain) NSMutableDictionary *studentStore;
当然,在实现文件中:
@implementation School
@synthesize studentStore;
我想将一个对象添加到字典中:
- (BOOL)addStudent:(Student *)newStudent
{
NSLog(@"adding new student");
[studentStore setObject:newStudent forKey:newStudent.adminNo];
return YES;
}
Student 类具有以下属性:
@interface学生:NSObject
{
@私人的
NSString* 名称; //属性
NSString* 性别;
年龄;
NSString* 管理员号;
}
其中 newStudent 具有以下值:
Student *newStudent = [[Student alloc] initWithName:@"jane" 性别:@"女" 年龄: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);
}
NSLog(@"printStudents failed");
}
它无法打印表中的值。相反,它打印行“printStudents failed”。
我想这是非常基本的,但由于我是 iOS 编程新手,所以我有点困惑。任何帮助将不胜感激。谢谢。