问题是删除使用writeToFile:方法编写的项,我似乎无法删除它。我尝试过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存储其他东西,如果是的话--如何删除用writeToFile编写的这个项?:
[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}
发布于 2013-07-10 11:18:36
如果您的文件位于该路径上,则可以尝试以下操作:
[[NSFileManager defaultManager] removeItemAtPath:[destinationURL path] error:&error];希望这能有所帮助。
发布于 2013-07-10 11:21:31
“出于某种原因”:您的" URL“只是一个路径,它不是一个有效的文件系统URL。为此,您需要在实际路径之前预先添加file://,甚至更好的是使用[NSURL fileURLWithPath:]。
发布于 2013-07-10 11:19:14
在这个地方
result = [fileManager removeItemAtURL:destinationURL error:&removeError];
NSLog(@"ERROR REMOVING OBJECT: %@",removeError);您可以进行日志记录,而不检查结果值。真的是NO吗?您应该先检查返回值,然后如果是NO,则检查removeError。
而且我更喜欢写
NSError * removeError = nil;在声明的时候。可能是包含一些旧信息的removeError对象。
https://stackoverflow.com/questions/17568890
复制相似问题