NSMutableDictionary setObject:forKey: 添加键失败 [英] NSMutableDictionary setObject:forKey: fails to add key

查看:56
本文介绍了NSMutableDictionary setObject:forKey: 添加键失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定我正在尝试编写的一个小 iPhone 程序中遗漏了一些东西,但代码很简单,它编译时没有任何错误,所以我看不到错误在哪里.

I'm sure I'm missing something in a small iPhone program I'm trying to write, but the code is simple and it compiles without any errors and so I fails to see where the error is.

我设置了一个 NSMutableDictionary 来存储学生的属性,每个属性都有一个唯一的键.在头文件中,我声明了 NSMutableDictonary studentStore:

I've set up a NSMutableDictionary to store students' attributes, each with a unique key. In the header file, I declare the NSMutableDictonary studentStore:

@interface School : NSObject
{
    @private
    NSMutableDictionary* studentStore;
}   

@property (nonatomic, retain) NSMutableDictionary *studentStore;

当然在实现文件中:

@implementation School
@synthesize studentStore;

我想在字典中添加一个对象:

And I want to add an object into the dictionary:

- (BOOL)addStudent:(Student *)newStudent
{
    NSLog(@"adding new student");
    [studentStore setObject:newStudent forKey:newStudent.adminNo];
    return YES;
}

类 Student 具有以下属性:@interface 学生:NSObject{@私人的NSString* 名称;//属性NSString* 性别;年龄;NSString* adminNo;}

class Student has the attributes: @interface Student : NSObject { @private NSString* name; //attributes NSString* gender; int age; NSString* adminNo; }

其中 newStudent 具有以下值:Student *newStudent = [[Student alloc] initWithName:@"jane" 性别:@"female" 年龄:16 adminNo:@"123"];

where newStudent has the values: Student *newStudent = [[Student alloc] initWithName:@"jane" gender:@"female" age: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"这一行.

It fails to print the values in the table. Instead, it prints the line "printStudents failed".

我想这是非常基本的,但由于我是 iOS 编程的新手,所以我有点困惑.任何帮助将不胜感激.谢谢.

I guess this's quite basic, but since I'm new to iOS programming I'm a bit stumped. Any help will be appreciated. Thanks.

推荐答案

你的 studentStore 实例变量是一个 NSMutableDictionary指针.默认情况下,它指向 nil,这意味着它不指向任何对象.您需要将其设置为指向 NSMutableDictionary 的一个实例.

Your studentStore instance variable is a pointer to an NSMutableDictionary. By default, it points to nil, meaning it doesn't point to any object. You need to set it to point to an instance of NSMutableDictionary.

- (BOOL)addStudent:(Student *)newStudent
{
    NSLog(@"adding new student");
    if (studentStore == nil) {
        studentStore = [[NSMutableDictionary alloc] init];
    }
    [studentStore setObject:newStudent forKey:newStudent.adminNo];
    return YES;
}

这篇关于NSMutableDictionary setObject:forKey: 添加键失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆