I'm sure I'm missing something obvious here in a simple iPhone program I'm trying to write, but the code seems so basic that I can't see what's wrong.

I'm trying to use an NSMutableDictionary to store a list of classes and their associated save file names. In the header file I declare the dictionary

@interface ClassList : UITableViewController {
NSString *homedirectory;
NSString *masterindexpath;
NSMutableDictionary *classFilenameGlossary;
NSMutableArray *listofclasses;

@property (retain, nonatomic) NSString *homedirectory;
@property (retain, nonatomic) NSString *masterindexpath;
@property (retain, nonatomic) NSMutableDictionary *classFilenameGlossary;
@property (retain, nonatomic) NSMutableArray *listofclasses;

And, of course, in the implementation file:

@implementation ClassList

@synthesize homedirectory;
@synthesize masterindexpath;
@synthesize classFilenameGlossary;
@synthesize listofclasses;

I initialize this dictionary at ViewDidLoad from an existing file that saves the classlist:

- (void)viewDidLoad {
[super viewDidLoad];

// Get home directory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
homedirectory = [paths objectAtIndex:0];
masterindexpath = [homedirectory stringByAppendingPathComponent:@"MasterIndex"];

// Get master course list or create it if necessary

NSFileManager *fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:masterindexpath] == NO)
    NSMutableDictionary *temporarydictionary = [NSMutableDictionary dictionary];
    [temporarydictionary writeToFile:masterindexpath atomically:NO];
    [temporarydictionary release];

[classFilenameGlossary initWithContentsOfFile:masterindexpath];

[homedirectory retain];
[masterindexpath retain];


Later, I add an item using setObject:forKey: but the dictionary never changes. Originally I was doing this in another view controller using a pointer back to this ClassList file, but once I discovered that didn't work, I simplified and tried to just set any sample object and key within the ClassList file:

    [classFilenameGlossary setObject:@"sample filename" forKey:@"sample classname"];

I've looked at the debugger variable list and it shows classFilenameGlossary properly identified as an NSMutableDictionary with 0 key/value pairs. I'm not getting any sort of error; the dictionary simply never changes. At the moment, the dictionary loaded from the file masterindexpath is empty (since I haven't been able to add any items yet), but that shouldn't keep me from being able to add items to the dictionary.

I'm a total beginner at iPhone programming, so I'm sure there's something basic that I'm missing. I'd appreciate any help.

No correct solution


This line:

[classFilenameGlossary initWithContentsOfFile:masterindexpath];

should look like this:

classFilenameGlossary = [[NSMutableDictionary alloc] initWithContentsOfFile:masterindexpath];

You forgot to allocate memory for the NSMutableDictionary, so that's why it never initializes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow