简体   繁体   English

如何从iOS应用沙箱删除目录

[英]How to delete directory from iOS app sandbox

Suppose I created a folder in "Documents" folder in my application sandbox and I called it "ID123". 假设我在应用程序沙箱的“文档”文件夹中创建了一个文件夹,并将其命名为“ ID123”。

From NSFileManager Class Reference I found out that I can create a new folder with one of these methods: 从NSFileManager类参考中,我发现可以使用以下方法之一创建新文件夹:

– createDirectoryAtURL:withIntermediateDirectories:attributes:error:
– createDirectoryAtPath:withIntermediateDirectories:attributes:error:

The question is how can I delete created directory? 问题是如何删除创建的目录?

You can use removeItemAtPath:error: OR removeItemAtURL:error: for doing this. 您可以使用removeItemAtPath:error: removeItemAtURL:error:进行此操作。

Like: 喜欢:

[[NSFileManager defaultManager] removeItemAtPath:yourPath error:nil];

or you can use: 或者您可以使用:

[[NSFileManager defaultManager] removeItemAtURL:yourPathURL error:nil];

Depending on whether you're using the URL or path based approach, either use the... 根据您使用的是URL还是基于路径的方法,请使用...

- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error

or 要么

- (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error

...NSFileManager method. ... NSFileManager方法。

For example: 例如:

NSError *removalError = nil;
if(![[NSFileManager defaultManager] removeItemAtPath:pathToFile error:&removalError]) {
    // Something went wrong.
    NSLog(@"%@", [removalError localizedDescription]);
}

In terms of recursive removal the supplied path (or URL depending on the method) can point at a directory containing items/sub-folders, etc. As per the Apple docs: 在递归删除方面,提供的路径(或URL,取决于方法)可以指向包含项目/子文件夹等的目录。根据Apple文档:

A path string indicating the file or directory to remove. 指示要删除的文件或目录的路径字符串。 If the path specifies a directory, the contents of that directory are recursively removed. 如果路径指定目录,那么将递归删除该目录的内容。

Incidentally, this is clearly defined within the "Creating and Deleting Items" section of the NSFileManager Class reference , so it would probably be worth the time to give that document a quick overview. 顺便说一句,这在NSFileManager类参考的“创建和删除项目”部分中进行了明确定义,因此值得花时间对该文档进行快速概述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM