iOS 小心刪除Documents中的檔案

來源:互聯網
上載者:User

在iOS開發中,刪除Sandbox中Documents目錄下的檔案可能是個比較常用的操作,下面是我封裝的部分代碼:

- (void)viewDidLoad{    [super viewDidLoad];        NSString *fileName = @"test";    NSString *filePath = [self getDirectoryOfDocumentFileWithName:fileName];    NSLog(@"%@", filePath);    if (filePath) {        [self removeFileAtPath:filePath];    }}- (NSString *)getDirectoryOfDocumentFolder {    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // 擷取所有Document檔案夾路徑    NSString *documentsPath = paths[0]; // 搜尋目標檔案所在Document檔案夾的路徑,通常為第一個        if (!documentsPath) {        NSLog(@"Documents目錄不存在");    }        return documentsPath;}- (NSString *)getDirectoryOfDocumentFileWithName:(NSString *)fileName {    NSString *documentsPath = [self getDirectoryOfDocumentFolder];    if (documentsPath) {        return [documentsPath stringByAppendingPathComponent:fileName]; // 擷取用於存取的目標檔案的完整路徑    }        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(@"移除檔案失敗,錯誤資訊:%@", error);        }        else {            NSLog(@"成功移除檔案");        }    }    else {        NSLog(@"檔案不存在");    }}

在viewDidLoad方法中,fileName給出了Documents檔案夾中的檔案名稱,然後擷取其完整路徑,並通過remove方法來移除。

在Documents下建立一個test檔案夾,運行後控制台輸出:

2014-03-15 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] 成功移除檔案

非常成功。


但是,假如fileName為空白,例如:

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

運行看看,控制台輸出如下:

2014-03-15 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] 成功移除檔案

開啟模擬器目錄:


可以看見整個Documents檔案夾都被刪除了。原因是如果為stringByAppendingPathComponent:方法傳遞的參數是@"",那麼返回的就是當前路徑,即Documents檔案夾的路徑。

這種行為非常非常的危險,要特別小心。


比較保險的方法是先做一個判斷:

- (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 isEqualToString:@""]) {        return nil;    }        NSString *documentsPath = [self getDirectoryOfDocumentFolder];    if (documentsPath) {        return [documentsPath stringByAppendingPathComponent:fileName]; // 擷取用於存取的目標檔案的完整路徑    }        return nil;}

當然也可以把判斷放在getDirectoryOfDocumentFileWithName:方法之外,這個視需求而定。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.