NSFileManager:enumeratorAtURL:返回不同形式的URL到NSFileManager:URLForDirectory [英] NSFileManager:enumeratorAtURL: returns a different form of URL to NSFileManager:URLForDirectory
问题描述
如果我使用 NSFileManager:URLForDirectory
获取我的沙箱的应用程序支持目录,那么我回到以下URL:
file:// localhost / var / mobile / Applications / EA80DE91-394C-4EAB-B269-1081C859BB0F / Library / Application%20Support /
/ pre>
然而,如果我使用
NSFileManager:enumeratorAtURL
搜索目录,我得到的形式的URL: / p>
file:// localhost / private / var / mobile / Applications / EA80DE91-394C-4EAB-B269-1081C859BB0F / Library / Application %20Support /
这会导致我的问题,因为我在做网址搜索/替换操作和差异导致不匹配。我之前遇到过这种区别,我的解决方案是使用urls的路径(使用
NSURL:path
),而不是原始的URL,但是在这种情况下不会工作,因为产生的路径是:/ var / mobile / Applications / EA80DE91-394C- 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]
includePropertiesForKeys: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:
以返回相同的路径,或者如果没有,是否有另一种优雅的方式提取子路径到我目前正在做什么?解决方案我不得不切换到这样做:
NSDirectoryEnumerator * dirEnum = fileManager enumeratorAtPath:[NSHomeDirectory()stringByAppendingPathComponent:@/ Library / Application Support /]];
If I fetch my sandbox's Application Support directory using
NSFileManager:URLForDirectory
then I get back the following URL:file://localhost/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application%20Support/
However if I use
NSFileManager:enumeratorAtURL
to search the directory I get back URLs of the form:file://localhost/private/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application%20Support/
This is causing me problems because I'm doing URL search/replace manipulations and the difference causes a mismatch. I've run into this sort of difference elsewhere previously and my solution then was to use the path of the urls (using
NSURL:path
) instead of the raw URLs, however that won't work in this situation as the paths produced are:/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application Support /private/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application Support/
What I am trying to do is this: I can have any number of files in my Application Support directory with the same name but different locations, and I need to extract the path relative to the Application Support directory. i.e. if I have the following
/Application Support/abc/file.dat /ApplicationSupport/abc/def/file.dat
I want to be able to extract "abc" and "abc/def". I was doing this using the following code:
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 calls NSFileManager:URLForDirectory: using NSApplicationSupportDirectory)
This works fine on the simulator where
enumeratorAtURL:
andURLFOrDirectory:
return identical paths, but due to their differences on hardware it fails.Is there a way to get
enumeratorAtURL:
andURLForDirectory:
to return identical paths, or if not is there an alternative elegant way of extracting the subpaths to what I am currently doing?解决方案I had to switch to doing this instead:
NSDirectoryEnumerator *dirEnum = [self.fileManager enumeratorAtPath: [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Application Support/"]];
这篇关于NSFileManager:enumeratorAtURL:返回不同形式的URL到NSFileManager:URLForDirectory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!