Skip to content

Instantly share code, notes, and snippets.

@hachinobu
hachinobu / gist:8453941
Created January 16, 2014 12:12
NSNumberの便利な使い方
NSNumber *intNum = @1;
NSNumber *floatNum = @1.23f;
NSNumber *doubleNum = @1.2345;
NSNumber *boolNum = @YES;
NSNumber *charNum = @'a';
int numInt = [intNum intValue];
float numFloat = [floatNum floatValue];
double numDouble = [doubleNum doubleValue];
BOOL numBool = [boolNum boolValue];
@hachinobu
hachinobu / gist:8454619
Created January 16, 2014 13:00
定数の定義方法
//ローカルな定数
static NSString * const kFirstName = @"hachinobu";
//グローバルな定数
//ヘッダーファイル内で宣言
extern NSString * const FirstName;
//実装ファイルで定義
NSString *const FirstName = @"hachinobu";
@hachinobu
hachinobu / サンプル
Created February 14, 2014 04:46
CGGeometryクラスの位置やサイズ操作系の関数紹介 ref: http://qiita.com/hachinobu/items/f8ac32870739c7d4eab8
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(40, 50, 180, 180)];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(70, 90, 180, 200)];
view2.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view2];
CGRect unionRect = CGRectUnion(view1.frame, view2.frame);
NSLog(@"unionRect:%@", NSStringFromCGRect(unionRect));
UIView *unionView = [[UIView alloc] initWithFrame:unionRect];
unionView.backgroundColor = [UIColor clearColor];
@hachinobu
hachinobu / gist:9361841
Created March 5, 2014 05:38
CoreDataよりNSFetchRequestをsetReturnsDistinctResults:YES、setResultType:NSDictionaryResultTypeで取得した際に返り値であるNSDictionaryをNSManagedObjectに変換する方法
- (id)initWithDictionary:(NSDictionary *)dict managedContext:(NSManagedObjectContext *)context
{
self = [super initWithEntity:[NSEntityDescription entityForName:[[self class] entityName] inManagedObjectContext:context] insertIntoManagedObjectContext:nil];
if (self) {
for (NSString *key in dict.allKeys) {
[self setValue:dict[key] forKey:key];
}
}
@hachinobu
hachinobu / UIImageResize
Created March 13, 2014 08:15
UIImageのリサイズ方法
//UIImageのリサイズ
- (UIImage *)resizeImage:(UIImage *)image rect:(CGRect)rect
{
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
[image drawInRect:rect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
UIGraphicsEndImageContext();
return resizedImage;
@hachinobu
hachinobu / gist:9594135
Last active August 29, 2015 13:57
UIWebViewで表示しているHTMLのソースコード取得
//全文
NSString* html = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].outerHTML"];
//bodyタグ内
NSString* html = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
@hachinobu
hachinobu / mainScreen
Created March 26, 2014 08:55
ステータスバー領域を含む画面の領域とステータスバー領域を含まない画面の領域の取得方法
//ステータスバー領域を含む画面の領域
CGRect bounds = [UIScreen mainScreen].bounds;
ステータスバー領域を含まない画面の領域
CGRect applicationFrame = [UIScreen mainScreen].applicationFrame;
@hachinobu
hachinobu / UIColorHex
Created March 27, 2014 06:47
16進数カラーコードからUIColorを作成する
+ (UIColor *)colorWithHexString:(NSString *)hex
{
//先頭に#がついていた場合は#を削除
if ([hex hasPrefix:@"#"]) {
hex = [hex substringFromIndex:1];
}
unsigned int rgb[3];
for (int i = 0; i < 3; i++) {
NSString *component = [hex substringWithRange:NSMakeRange(i * 2, 2)];
NSScanner *scanner = [NSScanner scannerWithString:component];
@hachinobu
hachinobu / gist:9802771
Created March 27, 2014 08:16
複数ファイルの一気にローカライズする方法
ja.lprojとen.lprojフォルダを作成してそれぞれの言語ファイルを同じ名前で入れる。
en.lprojフォルダ配下は自動で認識されるがja.lprojフォルダ配下は[Target Membership]にチェックが外れているのでチェックを入れればローカライズされる。
@hachinobu
hachinobu / gist:998b8ae26065b8e069c2
Created May 9, 2014 10:30
CoreDataでcount関数を使う
//集計対象
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"column1"];
//集計関数countを指定
NSExpression *countExpression = [NSExpression expressionForFunction:@"count:" arguments:@[keyPathExpression]];
//集計式の対象(NSExpressionDescription )
NSExpressionDescription *expressionDescription = [[[NSExpressionDescription alloc] init] autorelease];
[expressionDescription setName:@"count"];
[expressionDescription setExpression:countExpression];
[expressionDescription setExpressionResultType:NSInteger32AttributeType];