我正在使用一些用户在此处发布的代码修改我的设置来使用进度条进行文件复制(来自Cocoa是我的女朋友http://www.cimgf.com/2008/05/03/cocoa-tutorial-file-copy-with-progress-indicator/)。
我试图修复一些已弃用方法的使用,并且在此过程中已破坏检测文件大小。我昨天大部分时间都在尝试不同的东西来修复它,但没有任何运气。我还应该注意,sourceFilePath是从NSTextField获得的。
我知道错误消息说该文件不存在,但确实如此。它的路径不包含任何sym链接,并确保我使用NSString方法stringByResolvingSymlinksInPath。任何想法在这里出了什么问题?这是代码,以及我得到的错误消息。
- (IBAction)startCopy:(id)sender;
{
NSError *attributeserror = nil;
NSString *accountsourcestring = [sourceFilePath stringValue];
NSString *accountsourcestringpath =[accountsourcestring stringByStandardizingPath];
// Use the NSFileManager to obtain the size of our source file in bytes.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *sourceAttributes = [fileManager attributesOfItemAtPath:accountsourcestringpath error:&attributeserror];
NSNumber *sourceFileSize= [sourceAttributes objectForKey:NSFileSize];
long long fileSize = [sourceFileSize longLongValue];
NSLog(@"Filesize = %lld", fileSize);
NSLog(@"sourcefilepath: %@", accountsourcestring);
if ((sourceFileSize = [sourceAttributes objectForKey:NSFileSize]) )
{
// Set the max value to our source file size
[progressIndicator setMaxValue:(double)[sourceFileSize unsignedLongLongValue]];
}
else
{
// Couldn't get the file size so we need to bail.
NSLog(@"Unable to obtain size of file being copied. Error %@ Source file size: %@, sourceAttributes: %@", attributeserror, sourceFileSize, sourceAttributes);
return;
}
这是控制台中记录的错误消息:
2012-09-01 11:23:07.061 Copy File[1004:303] Filesize = 0
2012-09-01 11:23:07.062 Copy File[1004:303] sourcefilepath: file://localhost/Users/daniel/Lion%20plists/
2012-09-01 11:23:07.072 Copy File[1004:303] Unable to obtain size of file being copied. Error Error Domain=NSCocoaErrorDomain Code=260 "The file “Lion%20plists” couldn’t be opened because there is no such file." UserInfo=0x100173770 {NSFilePath=file:/localhost/Users/daniel/Lion%20plists, NSUnderlyingError=0x100140240 "The operation couldn’t be completed. No such file or directory"} Source file size: (null), sourceAttributes: (null)