我疯了吗?在10.5+中帮助NSFileManager委托方法shouldProceedAfterError

Chi*_*ief 4 cocoa objective-c nsfilemanager

所以我有点像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;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试处理的情况是文件是否已存在于目的地.我确实得到了一个错误,但我从来没有得到"更多错误......"跟踪输出.我也是从shouldCopyItemAtPath得到那个踪迹:所以我不确定为什么这个方法没有被触发?

我疯了,我怎么搞砸这里的委托实施?谢谢你的帮助!

Dew*_*sen 5

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

也许"如果你做了我们告诉你不做的事情,这不是一个错误."

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

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];
Run Code Online (Sandbox Code Playgroud)