Objective c NSFilemanager属性FiteMatPath挫折
我正在使用一个用户在这里发布的代码,该用户修改了我的设置,用一个进度条复制文件(来自Cocoa是我的女朋友) 我试图修复一些不推荐使用的方法,并在这个过程中破坏了检测文件大小的功能。昨天我花了一天的大部分时间尝试不同的方法来修复它,但没有任何运气。我还应该注意,sourceFilePath是从NSTextField获取的 我知道错误消息说文件不存在,但确实存在。它的路径不包含任何sym链接,为了确保我使用了NSString方法stringByResolvingSymlinksInPath。知道这里出了什么问题吗?这是代码和我收到的错误信息Objective c NSFilemanager属性FiteMatPath挫折,objective-c,Objective C,我正在使用一个用户在这里发布的代码,该用户修改了我的设置,用一个进度条复制文件(来自Cocoa是我的女朋友) 我试图修复一些不推荐使用的方法,并在这个过程中破坏了检测文件大小的功能。昨天我花了一天的大部分时间尝试不同的方法来修复它,但没有任何运气。我还应该注意,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)
为了变量名和可读性,建议使用eCamelCaps…是的,对不起。我的习惯是不戴骆驼帽,很难打破-(您的
sourcefilepath
包含百分比转义(“%20”),因此您应该使用stringbyreplacingpercentescapesusingencode:
来删除它。而且您的路径看起来更像一个目录。您的代码需要一些清理。删除NSString*
NSDictionary*
NSNumber*
等中出现的所有不必要的*
,然后深入到面向对象编程中。