我知道这个问题可能看起来与许多以前提出的问题相似,但是在阅读了所有这些问题和答案后,我无法理解该怎么做。
我想写一些有wordNames
andwordDefinitions
和 someID
和 a的单词date ID
。我有以下代码,但我有两个关于使用具有不同数据类型的数组的字典以及为字典定义键的方式的问题。
如果我制作的整个 .plist 文件有误,请纠正我。
提前致谢。
- (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];
}