NSMutableDictionary setObject:forKey: fails to add key

后端 未结 1 1441
夕颜
夕颜 2021-01-15 11:22

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 er

相关标签:
1条回答
  • 2021-01-15 11:54

    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;
    }
    
    0 讨论(0)
提交回复
热议问题