gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 04:02:58 25 4
gpt4 key购买 nike

所以我有点像 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com