Miniroo
Miniroo

Reputation: 425

Show contents of a folder Objective-C

I want to show the music files in a folder in Objective-C in a window so the user can see it.

It will always be the same folder, so I don't need to be able to browse.

I've tried NSBrowser, but the Apple SimpleCocoaBrowser example wasn't that simple, and did much more than I needed it to.

What would be the best way to do this? NSTableView?

Upvotes: 1

Views: 3790

Answers (1)

Thomas D.
Thomas D.

Reputation: 66

With this code, you get an array with all pdf's on the Desktop of the user named "Thomas". You can change the file extension to mp3 or something else. With an array controller you are able to fill this information into a tableview.

NSError *error = nil;

NSArray *desktopFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/Users/Thomas/Desktop" error:&error];

if(desktopFiles == nil){
   NSLog(@"%@", [error localizedDescription]);
   return 0;
}

NSArray *filteredDesktopFiles = [desktopFiles filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.pdf'"]];

for (id filename in filteredDesktopFiles){
    NSLog (@"Filename: %@", filename);
}

Upvotes: 4

Related Questions