我正在尝试在iPhone应用程序中创建一个“重启”功能,但我无法使用以下代码获取要删除的plist文件
- (IBAction)startOver:(id)sender {
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:@"data.plist" error:NULL];
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"welcome"];
[self presentViewController:controller animated:YES completion:nil];
}
有人能看出原因吗?
感谢。
答案 0 :(得分:2)
假设您将data.plist
保存在Documents
目录中,那么请执行以下操作:
- (NSString *)getFilePath {
NSArray *files = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[files objectAtIndex:0] stringByAppendingPathComponent:@"data.plist"];
}
- (IBAction)startOver:(id)sender {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path = [self getFilePath];
[fileManager removeItemAtPath:path error:NULL];
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"welcome"];
[self presentViewController:controller animated:YES completion:nil];
}