問題は、メソッドを使用して書き込まれたアイテム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}