事情是这样:我用的教程有点老,工具是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;
        }是转换问题还是文件问题?

解决方案 »

  1.   

    path 指的是你的文件路径,不是文件名。假设你的文件打包在主项目中:NSString* filePath = [[NSBundle mainBundle] pathForResource:"testFile" ofType:"xml"]; 
      

  2.   

    哪转换呢?用我之前那个? - (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error 方法中的的URL是NSURL型的,
      

  3.   

    这是这样改的 NSString        *filePath = [[NSBundle mainBundle] pathForResource: @"testfile" ofType: @"xml"];
    if ([fm  removeItemAtURL: [NSURL URLWithString: [filePath stringByAppendingFormat: filePath]] error: nil]== NO)
            {
                NSLog (@"File removal failed!");
                return 6;
            }程序执行通过了,不过文件还在没被删除
      

  4.   


    - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
      

  5.   

    或者用
    [fm removeItemAtURL: [NSURL fileURLWithPath:(NSString *)path]]试试,path是你以前的文件路径。