Suppose I created a folder in "Documents" folder in my application sandbox and I called it "ID123".
From NSFileManager Class Reference I found out that I can create a new folder with one of these methods:
– 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.
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...
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
or
- (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error
...NSFileManager method.
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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.