Created
February 12, 2013 09:41
-
-
Save mrtj/4761225 to your computer and use it in GitHub Desktop.
Utility class extension for creating random / temporary file names in one of the common directories of the current user's domain. #utility #nsstring #filename #random #objective-c
This file contains hidden or 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> | |
@interface NSString (RandomFileName) | |
+(NSString*)randomString; | |
+(NSString*)randomFileNameWithExtension:(NSString*)extension; | |
+(NSString*)randomFileNameInDirectory:(NSSearchPathDirectory)directory withExtension:(NSString*)extension; | |
@end |
This file contains hidden or 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 "NSString+RandomFileName.h" | |
@implementation NSString (RandomFileName) | |
+(NSString*)randomString | |
{ | |
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); | |
CFStringRef cfRandomString = CFUUIDCreateString(kCFAllocatorDefault, uuid); | |
CFRelease(uuid); | |
#if __has_feature(objc_arc) | |
NSString* randomString = (NSString*) CFBridgingRelease(cfRandomString); | |
#else | |
NSString* randomString = [((__bridge NSString*) cfRandomString) autorelease]; | |
#endif | |
return randomString; | |
} | |
+(NSString*)randomFileNameWithExtension:(NSString*)extension | |
{ | |
return [[NSString randomString] stringByAppendingPathExtension:extension]; | |
} | |
+(NSString*)randomFileNameInDirectory:(NSSearchPathDirectory)directory withExtension:(NSString*)extension | |
{ | |
NSString* path = [[[[NSFileManager defaultManager] URLsForDirectory:directory inDomains:NSUserDomainMask] lastObject] absoluteString]; | |
return [path stringByAppendingPathComponent:[NSString randomFileNameWithExtension:extension]]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment