2017-12-06 5 views
-1

ストーリーボードを使用してバーボタンアイテム(ゴミ箱)を作成します.Appは、ボタンアクションでドキュメントディレクトリからファイルを削除しようとするとクラッシュします。私はあなたが次のコードで最初のファイルがパスに存在するかどうかをチェックすべきだと思うobjective-cを使用してbarボタン項目を使用してドキュメントディレクトリからファイルを削除するにはどうすればよいですか?

@property (retain, nonatomic) IBOutlet UIBarButtonItem *deleteCsv; 



-(void)deleteCsv:(id)sender{ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

    NSString *filePath = [documentsPath stringByAppendingPathComponent:fileDataString4]; 
    NSError *error; 
    BOOL success = [fileManager removeItemAtPath:filePath error:&error]; 
    if (success) { 
     UIAlertView *removedSuccessFullyAlert = [[UIAlertView alloc] initWithTitle:@"Congratulations:" message:@"Successfully removed" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil]; 
     [removedSuccessFullyAlert show]; 
    } 
    else 
    { 
     NSLog(@"Could not delete file -:%@ ",[error localizedDescription]); 
    } 
+0

クラッシュやエラーに関する質問を投稿するときは、質問に以下を含める必要があります(質問を編集する、コメントを投稿しないでください):1 - 完全で正確なエラーメッセージ。 2 - エラーを引き起こすコードの正確な行を指摘してください。 – rmaddy

+0

@Alivajorg fileDataString4とは何ですか? – aNa12

+0

削除したいファイルの文字列が入っています@ aNa12 – Alivajorg

答えて

1

if ([[NSFileManager defaultManager] fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:<your file path>]]) 
{ 
    //Delete your file 
} 

これに従うことにより、あなたがそれならばクラッシュを処理することができます:ここではこれまでのコードがありますファイルが存在しないために発生している

関連する問題