Created
August 28, 2014 07:02
-
-
Save sgr/d854ab8abe2092773f73 to your computer and use it in GitHub Desktop.
UUIDをなるべく短い文字列に変換する ref: http://qiita.com/sgr@github/items/1e720c916f0c80dff4f4
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
// | |
// NSUUID+Base64.h | |
// | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSUUID (Base64) | |
- (instancetype)initWithBase64String:(NSString*)string withPadding:(BOOL)padding; | |
- (NSString*)Base64StringWithPadding:(BOOL)padding; | |
@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
// | |
// NSUUID+Base64.m | |
// | |
// | |
#import "NSUUID+Base64.h" | |
@implementation NSUUID (Base64) | |
- (instancetype)initWithBase64String:(NSString*)string withPadding:(BOOL)padding | |
{ | |
if (padding == NO) { | |
string = [string stringByPaddingToLength:(string.length + (string.length % 4)) withString:@"=" startingAtIndex:0]; | |
} | |
NSData* d = [[NSData alloc] initWithBase64EncodedString:string options:NSDataBase64DecodingIgnoreUnknownCharacters]; | |
uuid_t bytes; | |
[d getBytes:bytes]; | |
return [self initWithUUIDBytes:bytes]; | |
} | |
- (NSString*)Base64StringWithPadding:(BOOL)padding | |
{ | |
uuid_t bytes; | |
[self getUUIDBytes:bytes]; | |
NSString* string = [[NSData dataWithBytes:bytes length:sizeof(uuid_t)] | |
base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength]; | |
if (padding == NO) { | |
return [string stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"="]]; | |
} else { | |
return string; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment