Last active
August 29, 2015 14:04
-
-
Save aceontech/86904d8fc79895bdeb56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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