0

問題は、メソッドを使用して書き込まれたアイテムwriteToFile:を削除することです。削除できないようです。NSFileManager を試してみましたが、これらは 2 つの異なるタイプのストレージだと思います。

- (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;
}

NSDataのメソッドを使用してオブジェクトを追加します。plist を使用writeToFileして NSData を保存するため、これが問題であるに違いないと思いwriteToFileます。

[object writeToFile:destinationString atomically:YES];

ファイル削除時のエラーメッセージ

ERROR REMOVING OBJECT: Error Domain=NSCocoaErrorDomain Code=4 "The operation could't be completed. (Cocoa error 4.)" UserInfo=0xd4c4d40 {NSURL=/Users/bruker/Library/Application%20Support/iPhone%20Simulator/6.1/アプリケーション/14480FD3-9B0F-4143-BFA4-728774E7C952/Documents/FavoritesFolder/2innernaturemusic}

4

3 に答える 3

5

ファイルがそのパスに存在する場合は、これを試すことができます:

    [[NSFileManager defaultManager] removeItemAtPath:[destinationURL path] error:&error];

お役に立てれば。

于 2013-07-10T11:18:36.680 に答える
2

この場所で

result = [fileManager removeItemAtURL:destinationURL error:&removeError];
NSLog(@"ERROR REMOVING OBJECT: %@",removeError);

ログに記録し、結果値をチェックしません。それは本当にNOですか?最初に戻り値を確認し、それが である場合はNOを確認する必要がありremoveErrorます。

また、私は書くことを好みます

NSError * removeError = nil;

宣言するとき。removeErrorオブジェクトに古い情報が含まれている可能性があります。

于 2013-07-10T11:19:14.530 に答える