Проблема заключается в удалении элемента, который был написан с использованием метода writeToFile:
, я не могу его удалить. Я попробовал NSFileManager, но я думаю, что это два разных типа хранилища.NSFileManager: удалить объект
- (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
:
[object writeToFile:destinationString atomically:YES];
сообщение об ошибке удаления файла
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}
Выполняется ли любой NSLog? –
@Midhun: я забыл вставить сообщение об ошибке –
Вместо 'result = [fileManager removeItemAtURL: destinationURL error: & removeError];' use: 'result = [fileManager removeItemAtURL: [destinationURL path] error: & removeError];' –