2011-07-14 171 views
0

我正在研究一种方法,用于识别用户mac上的其他装入磁盘的kindle。 如果kindle无法通过名称识别,我的应用程序将通过查找包含文件类型的磁盘(例如.prc,.azw,.mobi,.mbp等)来决定安装哪个磁盘。等等)。这里是代码:objectForKey:NSFileType返回NULL

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++; 
    } 

不幸的是,当我运行下面的代码行时,返回NULL。

[[manager attributesOfItemAtPath:[subpaths objectAtIndex:currentSubpath] error:NULL] objectForKey:NSFileType] 

我与C++工作了很长一段时间,但我是相当新的目标C和可可,所以任何帮助将是非常感激的,我道歉提前如果这是一个noob问题。

+0

想通了。我没有将/ Volumes/DISKNAME添加到attributesOfPath –

+0

看起来像'enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:'('NSFileManager')的作业 – nielsbot

回答

1

而不是将NULL传递给error参数,将指针传递给NSError并查看返回的错误。例如:

NSError *error; 
[[manager attributesOfItemAtPath:[subpaths objectAtIndex:currentSubpath] error:&error] objectForKey:NSFileType]; 
NSLog(@"%@", error); 
+0

输出:“文件”system“无法打开,因为那里不是这样的文件。“底层错误=(错误域= NSPOSIXErrorDomain代码= 2“操作无法完成,没有这样的文件或目录” –

0

我建议检查你的“currentSubpath”变量,操作路径字符串时,很容易忘了(神圣)削减......