0

アプリにボタンがあり、クリックするとダイアログボックスが開きます。ユーザーがフォルダーを選択して [OK] をクリックすると、アプリはそのフォルダー内の PDF ファイルの数を表示します。

以下に次のコードを実装しました。フォルダー内の PDF ファイルの量をスキャンするにはどうすればよいですか?

- (IBAction)selectPathButton:(NSButton *)sender {

    // Loop counter.
    int i;

    // Create a File Open Dialog class.
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];

    // Set array of file types
    NSArray *fileTypesArray;
    fileTypesArray = [NSArray arrayWithObjects:@"pdf", nil];

    // Enable options in the dialog.
    [openDlg setCanChooseFiles:YES];
    [openDlg setAllowedFileTypes:fileTypesArray];
    [openDlg setAllowsMultipleSelection:TRUE];

    // Display the dialog box.  If the OK pressed,
    // process the files.
    if ( [openDlg runModal] == NSOKButton ) {

        // Gets list of all files selected
        NSArray *files = [openDlg URLs];

        // Loop through the files and process them.
        for( i = 0; i < [files count]; i++ ) {            
        }

        NSInteger payCount = [files count];
        self.payStubCountLabel.stringValue = [NSString stringWithFormat:@"%ld", (long)payCount];
    }
}
4

1 に答える 1

1

パスの下のファイルとディレクトリを取得します

[NSFileManager]- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error;

パスとサブパスの下のファイルとディレクトリを取得します

[NSFileManager]- (NSArray *)subpathsOfDirectoryAtPath:(NSString *)path error:(NSError **)error;

次に、PDFファイルを除外します。

NSMutableArray *pdfFiles = [NSMutableArray array];
for(NSString *fileName in files) {
    NSString *fileExt = [fileName pathExtension];
    if([fileExt compare:@"pdf" options:NSCaseInsensitiveSearch] == NSOrderSame) {
        [pdfFiles addObject:fileName];
    }
}

今あなたが欲しいものはpdfFilesにあります。

NSUInteger pdfFilesCount = [pdfFiles count];

PDFファイルの数だけが必要な場合は、forinループ付きの変数を使用します。

于 2013-02-22T06:58:27.523 に答える