Justin
Justin

Reputation: 2142

NSDictionary won't hold data properly

Basically, I am making an application that saves data into a custom file type. I have it setup to archive a dictionary and save that into a file. It then loads the file, unarchives the dictionary, and puts things where they should be.

Unfortunately, I cannot get my dictionary to actually save the data properly. I have two examples below.

Working

NSDictionary *theDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"test", @"test2", nil];

This will save "test" in the way I want it to, and I can load it perfectly.

Not working

NSString *aString = @"test";
NSDictionary *theDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:aString, @"test2", nil];

This just gives me a completely empty string when I load the file.

Here is my exact code.

- (NSData*)dataOfType:(NSString *)typeName error:(NSError **)outError {
    [fileContents release];

    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

    NSDictionary *theDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[lifeText stringValue], @"life", [moveText stringValue], @"move", nil];
    [archiver encodeObject:theDictionary forKey:@"SomeKeyValue"];
    [archiver finishEncoding];
    fileContents = [NSData dataWithData:data];
    [archiver release];

    return fileContents;
}

- (BOOL) readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
    fileContents = [data retain];

    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    loadedFileDictionary = [[unarchiver decodeObjectForKey:@"SomeKeyValue"] retain];

    [unarchiver finishDecoding];
    [unarchiver release];

    NSLog(@"%@", [loadedFileDictionary valueForKey:@"life"]);

    [Card_Building loadLife:[loadedFileDictionary valueForKey:@"life"] move:[loadedFileDictionary valueForKey:@"move"]];

    return YES;
}

The other string are set elsewhere for now, but I will need to put them here as well. Then, I use the following code in "Card Builder.m" to put the strings where I want them:

+ (void) loadLife:(NSString*)life move:(NSString*)move; {
    lifeString = life;
    moveString = move;
    importData = TRUE;
}

I don't see why this will not work. Every time I do test this, I get an empty string and the following error messages:

2011-06-09 13:33:56.082 HS-Cards[6479:a0f] *** Assertion failure in -[NSTextFieldCell _objectValue:forString:errorDescription:], /SourceCache/AppKit/AppKit-1038.35/AppKit.subproj/NSCell.m:1531

2011-06-09 13:33:56.082 HS-Cards[6479:a0f] Invalid parameter not satisfying: aString != nil

Would somebody please tell me why this won't work?

EDIT: with this updated code, it loads once and gives me the same error messages, but then it refused to load. Any ideas?

Upvotes: 0

Views: 625

Answers (1)

jscs
jscs

Reputation: 64002

If that's your exact code, then it looks like the problem is that you're trying to unarchive values for keys (move, range, attack, etc.) that you didn't archive to begin with. You're also putting lifeValue into the archived dictionary twice, which is unnecessary, because you're using the same key both times, and it has no real effect:

NSString *lifeValue = [lifeText stringValue];
NSDictionary *theDictionary = [[NSDictionary alloc] 
                                  initWithObjectsAndKeys:lifeValue, @"life", nil];
                                                       // ^^ Once
[theDictionary setObject:[lifeText stringValue] forKey:@"life"];
                         // ^^ Again; doesn't really do anything

Upvotes: 1

Related Questions