Objective c objectForKey:NSFileType返回NULL
我正在研究一种方法,可以从用户mac上其他安装的磁盘中识别kindle。 如果kindle无法通过名称识别,我的应用程序将通过查看并查找包含kindle使用的文件类型的磁盘(例如,.prc、.azw、.mobi、.mbp等)来确定哪个挂载的磁盘是kindle。代码如下:Objective c objectForKey:NSFileType返回NULL,objective-c,cocoa,Objective C,Cocoa,我正在研究一种方法,可以从用户mac上其他安装的磁盘中识别kindle。 如果kindle无法通过名称识别,我的应用程序将通过查看并查找包含kindle使用的文件类型的磁盘(例如,.prc、.azw、.mobi、.mbp等)来确定哪个挂载的磁盘是kindle。代码如下: NSLog(@"Scanning every file"); NSMutableArray *mountedDisks = [[NSMutableArray alloc] init]; mountedDi
NSLog(@"Scanning every file");
NSMutableArray *mountedDisks = [[NSMutableArray alloc] init];
mountedDisks = [workspace mountedRemovableMedia];
NSMutableArray *subpaths = [[NSMutableArray alloc] init];
int currentSubpath;
int proprietaryFilesFound;
while ([mountedDisks count] > currentDisk && [mountedDisks count] != 0)
{
subpaths = [manager subpathsAtPath:[mountedDisks objectAtIndex:currentDisk]];
currentSubpath = 0;
proprietaryFilesFound = 0;
NSLog(@"Entered outer loop");
while ([subpaths count] > currentSubpath && [subpaths count] != 0 && [[[manager attributesOfFileSystemForPath:[mountedDisks objectAtIndex:currentDisk] error:NULL] objectForKey:NSFileSystemSize] longLongValue] / 1073741824 <= 5 && proprietaryFilesFound < 7)
{
NSLog(@"Scanning %@, filetype is %@", [subpaths objectAtIndex:currentSubpath], [[manager attributesOfItemAtPath:[subpaths objectAtIndex:currentSubpath] error:NULL] objectForKey:NSFileType]);
if ([[[manager attributesOfItemAtPath:[subpaths objectAtIndex:currentSubpath] error:NULL] objectForKey:NSFileType] isEqual: @"azw"] || [[[manager attributesOfItemAtPath:[subpaths objectAtIndex:currentSubpath] error:NULL] objectForKey:NSFileType] isEqual: @"mbp"] || [[[manager attributesOfItemAtPath:[subpaths objectAtIndex:currentSubpath] error:NULL] objectForKey:NSFileType] isEqual: @"prc"] || [[[manager attributesOfItemAtPath:[subpaths objectAtIndex:currentSubpath] error:NULL] objectForKey:NSFileType] isEqual: @"mobi"])
{
proprietaryFilesFound++;
NSLog(@"Proprietary file found");
}
currentSubpath++;
}
currentDisk++;
}
我已经用C++工作了很长时间,但是我对目标C和COCOA比较新,所以任何帮助都会非常感谢,如果这是NoOB问题,我会提前道歉。
< P>不要把<代码> null >代码>传递给错误参数,把指针传递给<代码> NSError < /C> >,看看你得到了什么错误。例如:NSError *error;
[[manager attributesOfItemAtPath:[subpaths objectAtIndex:currentSubpath] error:&error] objectForKey:NSFileType];
NSLog(@"%@", error);
我建议检查“currentSubpath”变量,在处理路径字符串时,很容易忘记(神圣的)斜杠 输出:“无法打开文件“系统”,因为没有这样的文件。“基本错误=(错误域=NSPOSIXErrorDomain代码=2”操作无法完成。没有这样的文件或目录”我发现了。我没有将/Volumes/DISKNAME添加到attributesofpath中,这看起来像是
枚举器URL:includingPropertiesForKeys:options:errorHandler:
(NSFileManager
)
NSError *error;
[[manager attributesOfItemAtPath:[subpaths objectAtIndex:currentSubpath] error:&error] objectForKey:NSFileType];
NSLog(@"%@", error);