質問

どのように私は、ディレクトリ内のファイルを数えることができますか?私はNSFileManagerのクラス参照で関連する何かを見つけることができませんでした。

役に立ちましたか?

解決

contentsOfDirectoryAtPath:error:NSArrayを返します。ただ、配列にcountを送ります。

他のヒント

contentsOfDirectoryAtPath:error:がディレクトリ(ファイル+フォルダ)内のすべてののNSArrayを返します。

次のようにフィルタリングすることができる唯一のファイルの数を、取得するには

NSMutableArray *files = [[NSMutableArray alloc] init];
NSArray *itemsInFolder = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderpath error:NULL];

NSString *itemPath;
BOOL isDirectory;
for (NSString *item in itemsInFolder){
    itemPath = [NSString stringWithFormat:@"%@/%@", folderpath, item];
    [[NSFileManager defaultManager] fileExistsAtPath:item isDirectory:&isDirectory];
    if (!isDirectory) {
        [files addObject:item];
    }
}

return [files count];

<のhref = "https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/nsfilemanager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/ subpathsOfDirectoryAtPath%3aerror%3A」REL = "nofollowを"> subpathsOfDirectoryAtPath:エラー:ターゲットディレクトリのすべてのパスを持つ配列を返しのサブディレクトリを含む

しかし、ドキュメントが言うように、「あなたはパフォーマンス重視のコードでそれを使用したくない場合があります。」

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top