不能写字典到plist目标c

时间:2013-07-07 14:48:26

标签: iphone ios objective-c plist nsdictionary

通过搜索互联网,我想出了一些应该从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的数据,而不是上次运行时应该存储在其中的数据。

提前致谢。

1 个答案:

答案 0 :(得分:2)

问题是您正在尝试写入捆绑包中的文件。您可以写入的唯一位置是缓存目录(不是您想要的)和文档目录。

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"YourFile"];
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];

[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:path atomically:YES];