Skip to content

Instantly share code, notes, and snippets.

@matsuda
Last active October 17, 2015 04:24
Show Gist options
  • Save matsuda/1992429 to your computer and use it in GitHub Desktop.
Save matsuda/1992429 to your computer and use it in GitHub Desktop.
Objective-C NSString snippets
/*
ShiftJISのNSDataから文字コード変換してUTF-8の文字列へ
http://d.hatena.ne.jp/koherent/20100925/1285394783
*/
// ShiftJIS data
NSDate *data = ...;
// ShiftJIS string
NSString *str = [[[NSString alloc] initWithData:data encoding:NSShiftJISStringEncoding] autorelease];
// UTF-8のchar配列を取得
const char *chars = [str cStringUsingEncoding:NSUTF8StringEncoding];
// NSStringへ変換
NSString *string = [NSString stringWithCString:chars encoding:NSUTF8StringEncoding];
// Categoryで宣言
@interface UIView (Debug)
- (NSString *)recursiveDescription;
@end
NSLog(@"%@", [self.view recursiveDescription]);
@implementation NSString (CoreAdditions)
/*
URL encode
*/
- (NSString *)encodeURIComponent_NON_ARC
{
static NSString * const kLegalCharactersToBeEscaped = @"!*'();:@&=+$,/?%#[]";
return [(NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)self,
NULL,
(CFStringRef)kLegalCharactersToBeEscaped,
kCFStringEncodingUTF8) autorelease];
// AFNetworking
// static NSString * const kLegalCharactersToBeEscaped = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\|~ ";
// return [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
// (CFStringRef)[self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
// NULL,
// (CFStringRef)kLegalCharactersToBeEscaped,
// kCFStringEncodingUTF8) autorelease];
// http://akisute.com/2008/12/cocoaiphoneurl.html
// return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, CFSTR(";,/?:@&=+$#"), kCFStringEncodingUTF8);
}
- (NSString *)decodeURIComponent_NON_ARC
{
return [(NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
(CFStringRef)self,
CFSTR(""),
kCFStringEncodingUTF8) autorelease];
}
- (NSString *)encodeURIComponent
{
static NSString * const kLegalCharactersToBeEscaped = @"!*'();:@&=+$,/?%#[]";
return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef)self,
NULL,
(__bridge CFStringRef)kLegalCharactersToBeEscaped,
kCFStringEncodingUTF8);
}
- (NSString *)decodeURIComponent
{
return (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
(__bridge CFStringRef)self,
CFSTR(""),
kCFStringEncodingUTF8);
}
- (BOOL)matchWithPattern:(NSString *)pattern
{
NSLog(@"pattern >>>> %@", pattern);
NSRange match = [self rangeOfString:pattern options:NSRegularExpressionSearch];
if (match.location != NSNotFound) {
NSLog(@"<%@> is match <%@>", self, [self substringWithRange:match]);
return YES;
}
return NO;
}
/*
全角/半角変換
*/
- (NSString *)stringToHalfwidth
{
return [self stringTransformWithTransform:kCFStringTransformFullwidthHalfwidth reverse:false];
}
- (NSString *)stringToFullwidth
{
return [self stringTransformWithTransform:kCFStringTransformFullwidthHalfwidth reverse:true];
}
/*
iOS6のバグ
http://blog.livedoor.jp/tukuruyo/archives/52181393.html
*/
- (void)dummyTransform
{
[self stringTransformWithTransform:kCFStringTransformHiraganaKatakana reverse:false];
}
- (NSString *)stringTransformWithTransform:(CFStringRef)transform reverse:(Boolean)reverse
{
NSMutableString *retStr = [[NSMutableString alloc] initWithString:self];
CFStringTransform((__bridge CFMutableStringRef)retStr, NULL, transform, reverse);
return retStr;
}
/*
全角ハイフン->全角マイナス変換
*/
- (NSString *)safetyUnicodeString
{
unichar unichars[[self length]];
for (int i = 0, l = [self length]; i < l; i++) {
unichar c = [self characterAtIndex:i];
if (c == 0xff0d) {
unichars[i] = 0x2212;
} else {
unichars[i] = c;
}
}
NSString *text = [NSString stringWithCharacters:unichars length:[self length]];
/*
NSLog(@"text >>>>>>> %@", text);
for (int i = 0; i < [text length]; i++) {
unichar c = [text characterAtIndex:i];
NSLog(@"ccccc >>>>>> \\u%04x", c);
}
*/
return text;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment