今天我创建了一个带有本地数据存储(plist)的项目。一切正常,直到我重命名我的plist文件(从:Log.plist到:Data.plist)。我在Xcode中重命名它,所以没有错误的连接。
它可能是什么?我已经清除了Xcode,删除了DerivedData中的所有文件,但注意到了。数据不会保存在我的plist文件中。
还尝试使用其他名称重新创建项目或从Time Capsule恢复文件:每次重命名plist时,都不再存储任何内容。
有没有人知道为什么没有数据存储?谢谢你的时间。
- (void)viewDidLoad
{
[super viewDidLoad];
[self retrieveDataFromPlist:@"Data"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger results = tableViewData.count;
return results;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"logDataCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell.textLabel setText:[tableViewData objectAtIndex:indexPath.row]];
return cell;
}
- (void)retrieveDataFromPlist:(NSString *)plist
{
NSString *plistPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", plist]];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:plist ofType:@"plist"];
}
NSData *plistData = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *plistError;
NSPropertyListFormat plistFormat;
NSDictionary *plistDictionary = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&plistFormat errorDescription:&plistError];
if (!plistDictionary) {
NSLog(@"Error retrieving data from plist: %@", plistError);
}
tableViewData = [plistDictionary objectForKey:@"Logs"];
[self.logTableView reloadData];
}
- (void)storeData:(NSString *)data inPlist:(NSString *)plist
{
NSString *plistPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", plist]];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:plist ofType:@"plist"];
}
NSMutableArray *refreshedTableData = [NSMutableArray arrayWithCapacity:25];
for (int i = 0; i < tableViewData.count; i++) {
[refreshedTableData addObject:[tableViewData objectAtIndex:i]];
}
[refreshedTableData insertObject:data atIndex:0];
// remove last object after 25 logs to keep storage as small as possible
if (refreshedTableData.count > 25) {
[refreshedTableData removeLastObject];
}
NSDictionary *plistDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:refreshedTableData, nil] forKeys:[NSArray arrayWithObjects:@"Logs", nil]];
NSString *plistError;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:&plistError];
if (plistData) {
[plistData writeToFile:plistPath atomically:YES];
} else {
NSLog(@"Error storing data in plist: %@", plistError);
}
// retrieve refreshed data so tableView is always filled with the latest logs
[self retrieveDataFromPlist:@"Data"];
}
- (void)deleteFullPlist:(NSString *)plist
{
}
- (IBAction)menuButtonClicked:(id)sender
{
[self.slidingViewController slideRight];
}
- (IBAction)saveButtonClicked:(id)sender
{
[self storeData:self.textfield.text inPlist:@"Data"];
}
答案 0 :(得分:1)
无法将plist保存到应用程序的捆绑包中。
- (void)storeData:(NSString *)data inPlist:(NSString *)plist {
...
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:plist ofType:@"plist"];
}
删除上面的最后3行,您始终需要存储在Document目录中。