2012-07-18 11 views
11

如果我取使用NSFileManager:URLForDirectory然后我得到以下网址我的沙箱的应用支持目录:的NSFileManager:enumeratorAtURL:返回不同形式的URL来的NSFileManager:URLForDirectory

file://localhost/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application%20Support/ 

但是,如果我用NSFileManager:enumeratorAtURL搜索目录我找回形式的URL:

file://localhost/private/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application%20Support/ 

这引起了我的问题,因为我在做URL查找/替换操作和差异导致的不匹配。我在之前的其他地方遇到过这种差异,然后我的解决方案是使用URL的路径(使用NSURL:path)而不是原始URL,但在这种情况下无法运行,因为生成的路径为:

/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application Support 
/private/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application Support/ 

我想要做的是:我可以在我的应用程序支持目录中具有相同名称但位置不同的任意数量的文件,并且我需要提取相对于应用程序支持目录的路径。即如果我有以下内容

/Application Support/abc/file.dat 
/ApplicationSupport/abc/def/file.dat 

我想能够提取“abc”和“abc/def”。我在做这个使用下面的代码:

NSArray *keys = [NSArray arrayWithObject:NSURLIsRegularFileKey]; 
NSDirectoryEnumerator *dirEnumerator = [self.fileManager enumeratorAtURL:[MyManager appSupportDirectory] 
              includingPropertiesForKeys:keys 
                  options:NSDirectoryEnumerationSkipsHiddenFiles 
                 errorHandler:nil]; 
    for (NSURL *url in dirEnumerator) 
    { 
     if (NSOrderedSame == [[url lastPathComponent] caseInsensitiveCompare:kFilename]) 
     { 
      NSString *appSupportPath = [[MyManager appSupportDirectory] path]; 
      NSString *pathToFile = [url path]; 
      pathToFile = [pathToFile URLByDeletingLastPathComponent]; 
      NSString *subPath = [pathToFile stringByReplacingOccurrencesOfString:appSupportPath withString:@""]; 

(MyManager:appSupportDirectory调用的NSFileManager:URLForDirectory:使用NSApplicationSupportDirectory)

也能正常工作的模拟器,enumeratorAtURL:URLFOrDirectory:返回相同的路径,但由于其它在硬件上的差异失败。

有没有办法让enumeratorAtURL:URLForDirectory:返回相同的路径,或者如果没有其他优雅的方式提取子路径到我目前正在做的?

回答

3

我不得不切换到这样做,而不是:

NSDirectoryEnumerator *dirEnum = [self.fileManager enumeratorAtPath: [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Application Support/"]]; 
+0

我有同样的问题。 fileManager的contentsOfDirectoryAtURL返回一个带有/ private /的URL。我无法比较数据库中的URL记录。 – John 2012-10-31 13:05:23