Pregunta

I know the question might look similar to many previously-asked questions, but I haven't been able to understand what to do after reading all those questions and answers.

I want to write a number of words that have wordNames and wordDefinitions, and some ID and a date ID. I have the following code, but I have two questions about the dictionary using an array with different data types, and the way of defining keys for the dictionary.

Please correct me if the whole .plist file I'm making is wrong.

Thanks in advance.

- (IBAction)addWord:(id)sender
{
NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
destinationPath = [destinationPath stringByAppendingPathComponent:@"Box.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath:destinationPath]) 
{
    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"Box" ofType:@"plist"];
    [fileManager copyItemAtPath:sourcePath toPath:destinationPath error:nil];
}

// Load the Property List.  
NSMutableArray* wordsInTheBox = [[NSMutableArray alloc] initWithContentsOfFile:destinationPath];


NSString *wordName = word.name;
NSString *wordDefinition = word.definition;
NSInteger deckID;
NSDate addedDate;


//is this correct to have an array of different types?
NSArray *values = [[NSArray alloc] initWithObjects:wordName, wordDefinition, deckID, addedDate, nil]; 
//How and where am I supposed to define these keys?
NSArray *keys = [[NSArray alloc] initWithObjects: NAME_KEY, DEFINITION_KEY, DECK_ID_KEY, DATE_KEY, nil]; 
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
[wordsInTheBox addObject:dict];
[wordsInTheBox writeToFile:destinationPath atomically:YES];
}
¿Fue útil?

Solución

initWithContentsOfFile: always returns an immutable array. You should to this:

NSMutableArray *wordsInTheBox = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithContentsOfFile:destinationPath]];

What I don't entirely understand is where the word variable is defined. Is it an ivar?

If you're using the latest version of Xcode (4.4 or 4.5) I recommend using the much simpler literals for creating a dictionary.

NSDictionary *dict = @{NAME_KEY       : wordName, 
                       DEFINITION_KEY : wordDefinition, 
                       DECK_ID_KEY    : deckID, 
                       DATE_KEY       : addedDate};

But I don't see a problem with your dictionary definition either. It should work.

You have to make sure NAME_KEY, DEFINITION_KEY etc. are defined somewhere. All caps are usually only used for preprocessor macros, so you could do something like this:

#define NAME_KEY @"Name"
#define DEFINITION_KEY @"Definition"

You could also simply use the strings directly in your dictionary:

NSDictionary *dict = @{@"Name"       : wordName, 
                       @"Definition" : wordDefinition, 
                       @"DeckID"     : deckID, 
                       @"Date"       : addedDate};

But using macros isn't a bad idea either.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top