<分区>
通过搜索互联网,我找到了一些代码,可以从 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];
}
NSMutableDictionary
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 中的数据,而不是上次运行时应该存储在其中的数据。
提前致谢。