objective-c - 我疯了吗?帮助 NSFileManager 委托(delegate)方法 shouldProceedAfterError in 10.5+

标签 objective-c cocoa nsfilemanager

所以我有点像 Cocoa n00b,但我正在编写这个简单的小程序,但我无法触发 NSFileManager 委托(delegate)方法“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: 得到了那个踪迹:所以我不确定为什么这个方法没有触发?

我要疯了吗,我怎么弄乱了这里的委托(delegate)实现?感谢您的帮助!

最佳答案

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

即或许“如果你做了我们告诉你不要做的事情,那不是错误。”

您始终可以自行检查并删除它:

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];

关于objective-c - 我疯了吗?帮助 NSFileManager 委托(delegate)方法 shouldProceedAfterError in 10.5+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2103054/

相关文章:

ios - 重命名 iCloud 文档

iphone - Xcode UIProgressBar 不递增

objective-c - 非 rootViewController View 中的 NavigationController 问题

objective-c - 如何在 nsMenuItem Cocoa Mac osx 中对齐文本中心

cocoa - Mac OS X - 系统偏好设置 Pane 无法打开

ios - 如何使用 APFS 获取 iOS 10.3 上目录的大小?

ios - swift 设计模式。为什么他们包括 Any, AnyObject

iphone - 用于获取用户的 Facebook 好友性别以及 meet_sex 的 FQL 查询

iphone - 在 Objective-C 中动态实例化类,可能吗?

ios - 使用 [NSFileManager URLForUbiquityContainerIdentifier :] and [NSFileManager ubiquityIdentityToken]? 之间的主要区别是什么