0

I am attempting to create a 'start over' function in an iPhone application, but I cannot get a plist file to delete with the following code

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

Can anybody see why?

Thanks.

4

1 回答 1

2

假设您保存data.plistDocuments目录中,然后执行以下操作:

- (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];
}
于 2013-03-01T22:55:14.210 回答