我想删除文档目录的图像。该图像具有这种 2016-06-08 12:24:55.897image.jpg
类型的命名约定。
代码片段
-(void) removeImageAtPathNSString *) filePath{
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager removeItemAtPath:filePath error:&error];
if (success) {
NSLog(@"Image Successfully Deleted");
}
else{
NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
}
}
错误代码
NSCocoaErrorDomain 代码 = 4
我知道找不到文件时会出现错误。这是由于我使用的命名约定而发生的。 我不能改变约定。有什么办法仍然可以删除文件。
此代码直接删除文件名
- (void)removeImageNSString *)filename
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:filename];
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]);
}
}
其他明智的使用此代码
NSError *error;
if ([[NSFileManager defaultManager] isDeletableFileAtPath:path]) {
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
if (!success) {
NSLog(@"Error removing file at path: %@", error.localizedDescription);
}
}
你能试试这个代码吗
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL fileExists = [fileManager fileExistsAtPath:path];
NSLog(@"ath to file: %@", path);
NSLog(@"File exists: %d", fileExists);
NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:path]);
if (fileExists)
{
BOOL success = [fileManager removeItemAtPath:path error:&error];
if (!success) NSLog(@"Error: %@", [error localizedDescription]);
}
关于ios - 删除文件名中带有空格的文件 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37695584/
欢迎光临 OStack程序员社区-中国程序员成长平台 (http://ostack.cn/) | Powered by Discuz! X3.4 |