Created
January 28, 2014 23:41
-
-
Save itsthejb/8678922 to your computer and use it in GitHub Desktop.
Obj-C wrapper and categories for NSString and NSURL for Cocoa UTTypes
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
#import <Foundation/Foundation.h> | |
typedef NS_ENUM(NSUInteger, NSUTType) { | |
NSUTTypeImage, | |
NSUTTypeMovie, | |
NSUTTypeAudio, | |
NSUTTypeUnknown | |
}; | |
@interface NSURL (UTType) | |
- (NSUTType) UTType; | |
@end | |
@interface NSString (UTType) | |
- (NSUTType) UTType; | |
@end |
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
#import "Foundation+UTType.h" | |
@implementation NSURL (UTType) | |
- (NSUTType) UTType { return self.absoluteString.UTType; } | |
@end | |
@implementation NSString (UTType) | |
- (NSUTType) UTType { | |
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, | |
(__bridge CFStringRef)(self.pathExtension), | |
NULL); | |
static NSDictionary *map = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
map = @{ | |
(id) kUTTypeImage : @(NSUTTypeImage), | |
(id) kUTTypeMovie : @(NSUTTypeMovie), | |
(id) kUTTypeAudio : @(NSUTTypeAudio) | |
}; | |
}); | |
NSUTType ret = NSUTTypeUnknown; | |
for (NSString *type in map.allKeys) { | |
if (UTTypeConformsTo(UTI, (__bridge CFStringRef)(type))) { | |
ret = [map[type] integerValue]; | |
} | |
} | |
CFRelease(UTI); | |
return ret; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: expand to more types/subtypes