事情是这样:我用的教程有点老,工具是xcode 4.2,机器是X 10.7.3 。书中的部分语法已经弃用,其中有一个是删除文件的方法 - (BOOL)removeFileAtPath:(NSString *)path handler:(id)handler 已经被弃用,其替代者是 - (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error 问题就是我怎么把那个 (NSString *)path 转换成 (NSURL *)URL 。例子中的相关代码如下: NSString *fName = @"testfile"; if ([fm removeFileAtPath: fName handler: nil] == NO)
{
NSLog (@"File removal failed!");
return 6;
}我改成这样但是删不掉,也不确定对不对NSString *fName = @"testfile";if ([fm removeItemAtURL: [NSURL URLWithString: [fName stringByAppendingFormat: fName]] error: nil]== NO)
{
NSLog (@"File removal failed!");
return 6;
}是转换问题还是文件问题?
{
NSLog (@"File removal failed!");
return 6;
}我改成这样但是删不掉,也不确定对不对NSString *fName = @"testfile";if ([fm removeItemAtURL: [NSURL URLWithString: [fName stringByAppendingFormat: fName]] error: nil]== NO)
{
NSLog (@"File removal failed!");
return 6;
}是转换问题还是文件问题?
if ([fm removeItemAtURL: [NSURL URLWithString: [filePath stringByAppendingFormat: filePath]] error: nil]== NO)
{
NSLog (@"File removal failed!");
return 6;
}程序执行通过了,不过文件还在没被删除
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
[fm removeItemAtURL: [NSURL fileURLWithPath:(NSString *)path]]试试,path是你以前的文件路径。