Skip to content

Instantly share code, notes, and snippets.

@aceontech
Last active August 29, 2015 14:04
Show Gist options
  • Save aceontech/86904d8fc79895bdeb56 to your computer and use it in GitHub Desktop.
Save aceontech/86904d8fc79895bdeb56 to your computer and use it in GitHub Desktop.
@interface NSCBinarySearchExample() <>
@end
@implementation NSCBinarySearchExample
/**
* Find the index of a filename.
*/
- (NSUInteger)findFilename:(NSString *)filename
{
// List contents of resource directory
NSArray *resourceDirectoryListing = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self resourceDirectoryPath] error:&error];
// Sort alphabetically
resourceDirectoryListing = [resourceDirectoryListing sortedArrayUsingSelector:@selector(compare:)];
// Find filename index
NSUInteger index = [resourceDirectoryListing indexOfObject:filename
inSortedRange:NSMakeRange(0, [resourceDirectoryListing count])
options:NSBinarySearchingFirstEqual
usingComparator:^NSComparisonResult(NSString *filename1, NSString *filename2) {
return [filename1 compare:filename2];
}];
return index;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment