首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSFileManager:删除项目

NSFileManager:删除项目
EN

Stack Overflow用户
提问于 2013-07-10 10:57:51
回答 3查看 5.7K关注 0票数 0

问题是删除使用writeToFile:方法编写的项,我似乎无法删除它。我尝试过NSFileManager,但我想这是两种不同类型的存储。

代码语言:javascript
复制
- (BOOL) removeObject: (NSString *)objectToRemove inCategory:(StorageType)category
{
    BOOL result = NO;
    NSError *removeError;
    NSString *storageFolder = [self getCategoryFor:category];

    if (objectToRemove) {
        NSFileManager *fileManager = [[NSFileManager alloc]init];

        // Find folder
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:storageFolder];
        NSString *dataPathFormated = [dataPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
            NSLog(@"Removing file error: folder was not found");
        }

        NSURL *destinationURL = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@/%@",dataPathFormated,objectToRemove]];
        //NSString *destinationString = [NSString stringWithFormat:@"%@/%@",dataPath,objectToRemove];

        if ([fileManager fileExistsAtPath:[destinationURL path]]) {
            NSLog(@"destination URL for file to remove: %@", destinationURL);
            // Remove object
            result = [fileManager removeItemAtURL:destinationURL error:&removeError];
            NSLog(@"ERROR REMOVING OBJECT: %@",removeError);
        } else {
            NSLog(@"Object to remove was not found at given path");
        }
    }
    return result;
}

我使用writeToFile方法NSData添加对象,我想这一定是问题所在,因为它使用plist存储其他东西,如果是的话--如何删除用writeToFile编写的这个项?:

代码语言:javascript
复制
[object writeToFile:destinationString atomically:YES];

错误信息删除文件

错误删除对象:错误Domain=NSCocoaErrorDomain Code=4“操作无法完成。(可可错误4。)UserInfo=0xd4c4d40 {NSURL=/Users/bruker/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/14480FD3-9B0F-4143-BFA4-728774E7C952/Documents/FavoritesFolder/2innernaturemusic}

EN

回答 3

Stack Overflow用户

发布于 2013-07-10 11:18:36

如果您的文件位于该路径上,则可以尝试以下操作:

代码语言:javascript
复制
    [[NSFileManager defaultManager] removeItemAtPath:[destinationURL path] error:&error];

希望这能有所帮助。

票数 5
EN

Stack Overflow用户

发布于 2013-07-10 11:21:31

“出于某种原因”:您的" URL“只是一个路径,它不是一个有效的文件系统URL。为此,您需要在实际路径之前预先添加file://,甚至更好的是使用[NSURL fileURLWithPath:]

票数 4
EN

Stack Overflow用户

发布于 2013-07-10 11:19:14

在这个地方

代码语言:javascript
复制
result = [fileManager removeItemAtURL:destinationURL error:&removeError];
NSLog(@"ERROR REMOVING OBJECT: %@",removeError);

您可以进行日志记录,而不检查结果值。真的是NO吗?您应该先检查返回值,然后如果是NO,则检查removeError

而且我更喜欢写

代码语言:javascript
复制
NSError * removeError = nil;

在声明的时候。可能是包含一些旧信息的removeError对象。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17568890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档