2012-07-18 8 views
11

私はNSFileManager:URLForDirectoryを使用して、私のサンドボックスのApplication Supportディレクトリを取得した場合、私は戻って、次のURLを取得します:NSFileManager:enumeratorAtURL:NSFileManagerにURLの異なる形式を返します。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の代わりにURLのパス(NSURL:pathを使用)を使用することでしたが、生成されるパスがこのような状況では機能しません:

/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」ことができるようにしたいです。これはenumeratorAtURL:URLFOrDirectory:が同一のパスを返すシミュレータ上で正常に動作します

、しかし、それらに

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:@""]; 

(NSApplicationSupportDirectoryを使用してMyManager:appSupportDirectoryはNSFileManagerを呼び出します:URLForDirectory)私は、次のコードを使用して、これをやっていましたハードウェアの相違は失敗します。

enumeratorAtURL:URLForDirectory:に同じパスを返す方法はありますか?そうでない場合は、私が現在行っていることにサブパスを抽出する別の洗練された方法がありますか?

答えて

3

私の代わりにこれを行うに切り替える必要があった:

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

私は同じ問題を抱えていました。 fileManagerのcontentsOfDirectoryAtURLは、/ private /を含むURLを返します。データベースのURLレコードと比較するのに問題がありました。 – John

関連する問題