NSFileManager:删除项目 [英] NSFileManager: removing item

查看:96
本文介绍了NSFileManager:删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The problem is removing an item that´s been written using writeToFile: method, I cannot seem to remove it. I tried NSFileManager but I guess these are two different types of storage.

- (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来存储NSData其他东西,如果是 - 如何删除用 writeToFile写的项目?:

I add the objects using the writeToFile method of NSData, I guess this must be the problem, since it uses plist to store while NSData something else, if it is - how do I remove this item written with writeToFile ?:

[object writeToFile:destinationString atomically:YES];

删除文件的错误消息


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

ERROR REMOVING OBJECT: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0xd4c4d40 {NSURL=/Users/bruker/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/14480FD3-9B0F-4143-BFA4-728774E7C952/Documents/FavoritesFolder/2innernaturemusic}


推荐答案

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

And the "for some reason": your "URL" is just a path, it's not a valid filesystem URL. For that, you would need to prepend file:// before the actual path, or even better, use [NSURL fileURLWithPath:].

这篇关于NSFileManager:删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆