IOS Delete objects in Documents with caution

Source: Internet
Author: User

In iOS development, deleting files under the Documents directory in Sandbox may be a common operation. Below is some of my encapsulated code:

-(Void) viewDidLoad {[super viewDidLoad]; NSString * fileName = @ "test"; NSString * filePath = [self getDirectoryOfDocumentFileWithName: fileName]; NSLog (@ "% @", filePath); if (filePath) {[self removeFileAtPath: filePath] ;}}- (NSString *) getDirectoryOfDocumentFolder {NSArray * paths = require (NSDocumentDirectory, NSUserDomainMask, YES ); // obtain the path NSString * docume for all Document folders NtsPath = paths [0]; // search for the path of the Document folder where the target file is located, usually the first if (! DocumentsPath) {NSLog (@ "invalid ents directory does not exist");} return documentsPath;}-(NSString *) getDirectoryOfDocumentFileWithName :( NSString *) fileName {NSString * documentsPath = [self getDirectoryOfDocumentFolder]; if (documentsPath) {return [documentsPath stringByAppendingPathComponent: fileName]; // obtain the complete path of the target file for access} return nil;}-(BOOL) isFileExitsAtPath :( NSString *) filePath {NSFileManager * fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath: filePath isDirectory: NULL]) {return YES;} return NO;}-(void) removeFileAtPath :( NSString *) filePath {NSError * error = nil; if ([self isFileExitsAtPath: filePath]) {[[NSFileManager defaultManager] removeItemAtPath: filePath error: & error]; if (error) {NSLog (@ "failed to remove file, error message: % @", error);} else {NSLog (@ "successfully removed file ");}} else {NSLog (@ "file does not exist ");}}

In the viewDidLoad method, fileName provides the file name in the Documents folder, obtains its complete path, and removes it using the remove Method.

Create a new test folder under Documents, and then output it on the console:

13:02:54. 527 RemoveDocument [849: 70b]/Users/apple/Library/Application Support/iPhone Simulator/7.0.3/Applications/AA0DC0B6-EED1-4F2F-B470-326B7A5CB656/Documents/test2014-03-15 13:02:54. 529 RemoveDocument [849: 70b] the file is successfully removed.

Very successful.


However, if fileName is blank, for example:

//    NSString *fileName = @"test";    NSString *fileName = @"";

Run the command. The console output is as follows:

13:01:14. 381 RemoveDocument [816: 70b]/Users/apple/Library/Application Support/iPhone Simulator/7.0.3/Applications/AA0DC0B6-EED1-4F2F-B470-326B7A5CB656/Documents2014-03-15 13:01:14. 383 RemoveDocument [816: 70b] the file is successfully removed.

Open the simulator directory:


You can see that the entire Documents folder has been deleted. The reason is that if it is stringByAppendingPathComponent: The parameter passed by the method is @ "", the current path is returned, that is, the path of the Documents folder.

This kind of behavior is very dangerous, so be very careful.


The insurance method is to make a judgment first:

-(Void) viewDidLoad {[super viewDidLoad]; // NSString * fileName = @ "test"; NSString * fileName = @ ""; NSString * filePath = [self getDirectoryOfDocumentFileWithName: fileName]; NSLog (@ "% @", filePath); if (filePath) {[self removeFileAtPath: filePath] ;}}- (NSString *) getDirectoryOfDocumentFileWithName :( NSString *) fileName {if ([fileName isw.tostring: @ ""]) {return nil;} NSString * documentsPath = [self getDirectoryOfDocumentFolder]; if (documentsPath) {return [documentsPath stringbyappendpathcomponent: fileName]; // obtain the complete path of the target file for access} return nil ;}

Of course, you can also put the judgment in addition to the getDirectoryOfDocumentFileWithName: method, which depends on your needs.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.