iphone - 无法将字典写入 plist objective-c

标签 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 中的数据,而不是上次运行时应该存储在其中的数据。

提前致谢。

最佳答案

问题是您正在尝试写入包中的文件。唯一允许写入的地方是缓存目录(不是你想要的)和文档目录。

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];

关于iphone - 无法将字典写入 plist objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17513171/

相关文章:

iphone - 移动媒体查询 - 宽度不正确

iphone - 单击输入按钮或搜索按钮时隐藏键盘

ios - 根据每个段中的标题更改分段控件的宽度?

objective-c - 交错的 VBO 导致 glDrawArrays() 错误

ios - NSNotificationCenter addObserver 被忽略

ios - 适用于 iOS 的 Paypal SDK

ios - 防止在设备旋转时调用 MKMapViewDelegate 方法

iphone - 将 Pdf 拆分为页面以在 UIPageViewController 上显示

iphone - 一个 Xib 文件用于 2 种状态 : showing & editing a object

ios - 在drawInRect中绘制渐变层