通过搜索互联网,我想出了一些应该从 plist 中检索数据并将数据写入 plist 的代码。我可以很好地检索数据,但存储数据不起作用。这是我的商店数据方法:
- (void)saveData {
[self.time setObject:[NSNumber numberWithInt:self.brace.timeOutOfBrace.todayHours] forKey:@"TimeOutOfBraceHours"];
[self.time setObject:[NSNumber numberWithInt:self.brace.timeOutOfBrace.todayMinutes] forKey:@"TimeOutOfBraceMinutes"];
[self.time setObject:[NSNumber numberWithInt:self.brace.timeOutOfBrace.overallHours] forKey:@"OverallHours"];
[self.time setObject:[NSNumber numberWithInt:self.brace.timeOutOfBrace.overallMinutes] forKey:@"OverallMinutes"];
[self.time setObject:[NSNumber numberWithInt:self.brace.timeOutOfBrace.goalForWeek] forKey:@"WeeklyGoal"];
NSString *plistCatPath = [[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"];
NSMutableDictionary *rootDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistCatPath];
[rootDictionary setObject:self.time forKey:@"Time"];
[rootDictionary writeToFile:plistCatPath atomically:YES];
}
NSMutable 字典
self.time
在实现中声明。
这是读取数据的方法:
- (void)retreiveData {
NSString *plistCatPath = [[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"];
NSMutableDictionary *rootDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistCatPath];
self.time = rootDictionary[@"Time"];
NSLog(@"%@", self.time[@"TimeOutOfBraceHours"]);
self.brace.timeOutOfBrace.todayHours = [[self.time objectForKey:@"TimeOutOfBraceHours"] intValue];
self.brace.timeOutOfBrace.todayMinutes = [[self.time objectForKey:@"TimeOutOfBraceMinutes"] intValue];
self.brace.timeOutOfBrace.overallHours = [[self.time objectForKey:@"OverallHours"] intValue];
self.brace.timeOutOfBrace.overallMinutes = [[self.time objectForKey:@"OverallMinutes"] intValue];
self.brace.timeOutOfBrace.goalForWeek = [[self.time objectForKey:@"WeeklyGoal"] intValue];
}
当我运行程序时,它只是检索我手动放入 plist 的数据,而不是上次运行时应该存储在其中的数据。
提前致谢。