Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 我疯了吗?NSFileManager委托方法的帮助应在10.5+;_Objective C_Cocoa_Nsfilemanager - Fatal编程技术网

Objective c 我疯了吗?NSFileManager委托方法的帮助应在10.5+;

Objective c 我疯了吗?NSFileManager委托方法的帮助应在10.5+;,objective-c,cocoa,nsfilemanager,Objective C,Cocoa,Nsfilemanager,所以我有点像Cocoa n00b,但我正在编写这个简单的小程序,我无法启动NSFileManager委托方法“shouldProceedAfterError…”。下面是我在AppDelegate中运行的代码 -(BOOL)copyFile { [[NSFileManager defaultManager] setDelegate:self]; NSError *copyError = nil; NSString *filename = [[NSString alloc]

所以我有点像Cocoa n00b,但我正在编写这个简单的小程序,我无法启动NSFileManager委托方法“shouldProceedAfterError…”。下面是我在AppDelegate中运行的代码

-(BOOL)copyFile {
    [[NSFileManager defaultManager] setDelegate:self];

    NSError *copyError = nil;
    NSString *filename = [[NSString alloc] initWithString:[[[self.sourceFile path] componentsSeparatedByString:@"/"] lastObject]];
    NSString *destination = [[[[[UserData sharedData] folderLocation] path] stringByAppendingString:@"/"] stringByAppendingString:filename];

    [[NSFileManager defaultManager] copyItemAtPath:[self.sourceFile path] toPath:destination error:&copyError];

    NSLog(@"error! %@",copyError);

    [filename release];
    return YES;
}

- (BOOL)fileManager:(NSFileManager *)fileManager shouldProceedAfterError:(NSError *)error copyingItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath {
    NSLog(@"more error... %@",error);
    return NO;
}
- (BOOL)fileManager:(NSFileManager *)fileManager shouldCopyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath {
    NSLog(@"in shouldCopyItemAtPath...");
    return YES;
}
我试图处理的情况是,如果文件在目标位置已经存在。我确实得到了一个错误,但我从未得到输出的“更多错误…”跟踪。我也从shouldCopyItemAtPath获得了这个跟踪:所以我不确定为什么这个方法没有触发


我疯了吗?我是怎么搞砸了这里的代理实现的?谢谢你的帮助

这只是一个假设,但由于copyItemAtPath:toPath:error的定义是“在srcPath中指定的文件必须存在,而dstPath在操作之前不能存在”,因此dstPath已经存在的场景可能不被视为“错误”,因此不会触发委托


i、 e.也许“如果你做了我们告诉你不要做的事情,这不是一个错误。”

您可以随时进行检查并自己删除:

NSFileManager* fileManager = [NSFileManager defaultManager];

// Delete the file if it already exists.
if ([fileManager fileExistsAtPath: destination])
        if (![fileManager removeItemAtPath: destination error: error])
                return NO;

return [fileManager copyItemAtPath: source toPath: destination error: error];

可能您提供的源路径错误?
copyItemAtPath
如果源路径无效,则不会调用委托方法。
如果使用以下方法,则可以进行测试:

-(IBAction)copyFile:(id)sender
{
    [[NSFileManager defaultManager] setDelegate:self];
    NSError* copyError = nil;
    NSString* sourceFilepath = [@"~/Desktop/source.txt" stringByExpandingTildeInPath];
    NSString* targetFilepath = [@"~/Desktop/target.txt" stringByExpandingTildeInPath];  
    [[NSFileManager defaultManager] copyItemAtPath:sourceFilepath toPath:targetFilepath error:&copyError];  
    NSLog(@"Error:%@", copyError);  
}
调用该方法时,我注意到以下行为:

  • 如果~/Desktop/source.txt是文件且~/Desktop/target.txt不存在:
    • NSFileManager调用
      shouldCopyItemAtPath
      delegate方法
  • 如果~/Desktop/source.txt是一个文件并且~/Desktop/target.txt存在:
    • NSFileManager首先调用
      shouldCopyItemAtPath
      shouldProceedAfterError
  • 如果~/Desktop/source.txt不存在
    • NSFileManager不调用任何委托方法,只返回一个NSError对象

“如果你做了我们告诉你不要做的事情,这不是错误。”doh。我一定是完全错过了那部分。我希望我可以在委托方法中实现某种模式窗口,但你是对的,我只需要在开始复制之前检查一下。谢谢你的回复!我会投赞成票,但我没有任何代表:(